2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.lucene.benchmark.quality.utils;
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.text.NumberFormat;
23 import org.apache.lucene.benchmark.quality.QualityQuery;
24 import org.apache.lucene.search.ScoreDoc;
25 import org.apache.lucene.search.Searcher;
26 import org.apache.lucene.search.TopDocs;
29 * Create a log ready for submission.
30 * Extend this class and override
31 * {@link #report(QualityQuery, TopDocs, String, Searcher)}
32 * to create different reports.
34 public class SubmissionReport {
36 private NumberFormat nf;
37 private PrintWriter logger;
41 * Constructor for SubmissionReport.
42 * @param logger if null, no submission data is created.
43 * @param name name of this run.
45 public SubmissionReport (PrintWriter logger, String name) {
48 nf = NumberFormat.getInstance();
49 nf.setMaximumFractionDigits(4);
50 nf.setMinimumFractionDigits(4);
54 * Report a search result for a certain quality query.
55 * @param qq quality query for which the results are reported.
56 * @param td search results for the query.
57 * @param docNameField stored field used for fetching the result doc name.
58 * @param searcher index access for fetching doc name.
59 * @throws IOException in case of a problem.
61 public void report(QualityQuery qq, TopDocs td, String docNameField, Searcher searcher) throws IOException {
65 ScoreDoc sd[] = td.scoreDocs;
67 DocNameExtractor xt = new DocNameExtractor(docNameField);
68 for (int i=0; i<sd.length; i++) {
69 String docName = xt.docName(searcher,sd[i].doc);
71 qq.getQueryID() + sep +
73 format(docName,20) + sep +
74 format(""+i,7) + sep +
75 nf.format(sd[i].score) + sep +
87 private static String padd = " ";
88 private String format(String s, int minLen) {
89 s = (s==null ? "" : s);
90 int n = Math.max(minLen,s.length());
91 return (s+padd).substring(0,n);