1 package org.apache.lucene.benchmark.quality.trec;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.BufferedReader;
22 import java.io.FileReader;
23 import java.io.PrintWriter;
24 import java.util.HashSet;
27 import org.apache.lucene.benchmark.quality.Judge;
28 import org.apache.lucene.benchmark.quality.QualityBenchmark;
29 import org.apache.lucene.benchmark.quality.QualityQuery;
30 import org.apache.lucene.benchmark.quality.QualityQueryParser;
31 import org.apache.lucene.benchmark.quality.QualityStats;
32 import org.apache.lucene.benchmark.quality.utils.SimpleQQParser;
33 import org.apache.lucene.benchmark.quality.utils.SubmissionReport;
34 import org.apache.lucene.index.IndexReader;
35 import org.apache.lucene.search.IndexSearcher;
36 import org.apache.lucene.store.FSDirectory;
42 public class QueryDriver {
43 public static void main(String[] args) throws Exception {
44 if (args.length < 4 || args.length > 5) {
45 System.err.println("Usage: QueryDriver <topicsFile> <qrelsFile> <submissionFile> <indexDir> [querySpec]");
46 System.err.println("topicsFile: input file containing queries");
47 System.err.println("qrelsFile: input file containing relevance judgements");
48 System.err.println("submissionFile: output submission file for trec_eval");
49 System.err.println("indexDir: index directory");
50 System.err.println("querySpec: string composed of fields to use in query consisting of T=title,D=description,N=narrative:");
51 System.err.println("\texample: TD (query on Title + Description). The default is T (title only)");
55 File topicsFile = new File(args[0]);
56 File qrelsFile = new File(args[1]);
57 SubmissionReport submitLog = new SubmissionReport(new PrintWriter(args[2]), "lucene");
58 FSDirectory dir = FSDirectory.open(new File(args[3]));
59 IndexReader r = IndexReader.open(dir, true);
60 IndexSearcher searcher = new IndexSearcher(r);
61 String fieldSpec = args.length == 5 ? args[4] : "T"; // default to Title-only if not specified.
63 int maxResults = 1000;
64 String docNameField = "docname";
66 PrintWriter logger = new PrintWriter(System.out, true);
68 // use trec utilities to read trec topics into quality queries
69 TrecTopicsReader qReader = new TrecTopicsReader();
70 QualityQuery qqs[] = qReader.readQueries(new BufferedReader(new FileReader(topicsFile)));
72 // prepare judge, with trec utilities that read from a QRels file
73 Judge judge = new TrecJudge(new BufferedReader(new FileReader(qrelsFile)));
75 // validate topics & judgments match each other
76 judge.validateData(qqs, logger);
78 Set<String> fieldSet = new HashSet<String>();
79 if (fieldSpec.indexOf('T') >= 0) fieldSet.add("title");
80 if (fieldSpec.indexOf('D') >= 0) fieldSet.add("description");
81 if (fieldSpec.indexOf('N') >= 0) fieldSet.add("narrative");
83 // set the parsing of quality queries into Lucene queries.
84 QualityQueryParser qqParser = new SimpleQQParser(fieldSet.toArray(new String[0]), "body");
87 QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
88 qrun.setMaxResults(maxResults);
89 QualityStats stats[] = qrun.execute(judge, submitLog, logger);
91 // print an avarage sum of the results
92 QualityStats avg = QualityStats.average(stats);
93 avg.log("SUMMARY", 2, logger, " ");