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;
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.index.TermEnum;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.store.FSDirectory;
27 import org.apache.lucene.util.PriorityQueue;
30 * Suggest Quality queries based on an index contents.
31 * Utility class, used for making quality test benchmarks.
33 public class QualityQueriesFinder {
35 private static final String newline = System.getProperty("line.separator");
36 private Directory dir;
39 * Constructor over a directory containing the index.
40 * @param dir directory containing the index we search for the quality test.
42 private QualityQueriesFinder(Directory dir) {
47 * @param args {index-dir}
48 * @throws IOException if cannot access the index.
50 public static void main(String[] args) throws IOException {
52 System.err.println("Usage: java QualityQueriesFinder <index-dir>");
55 QualityQueriesFinder qqf = new QualityQueriesFinder(FSDirectory.open(new File(args[0])));
56 String q[] = qqf.bestQueries("body",20);
57 for (int i=0; i<q.length; i++) {
58 System.out.println(newline+formatQueryAsTrecTopic(i,q[i],null,null));
62 private String [] bestQueries(String field,int numQueries) throws IOException {
63 String words[] = bestTerms("body",4*numQueries);
66 String res[] = new String[m];
67 for (int i=0; i<res.length; i++) {
68 res[i] = words[i] + " " + words[m+i]+ " " + words[n-1-m-i] + " " + words[n-1-i];
69 //System.out.println("query["+i+"]: "+res[i]);
74 private static String formatQueryAsTrecTopic (int qnum, String title, String description, String narrative) {
77 "<num> Number: " + qnum + newline + newline +
78 "<title> " + (title==null?"":title) + newline + newline +
79 "<desc> Description:" + newline +
80 (description==null?"":description) + newline + newline +
81 "<narr> Narrative:" + newline +
82 (narrative==null?"":narrative) + newline + newline +
86 private String [] bestTerms(String field,int numTerms) throws IOException {
87 PriorityQueue<TermDf> pq = new TermsDfQueue(numTerms);
88 IndexReader ir = IndexReader.open(dir, true);
90 int threshold = ir.maxDoc() / 10; // ignore words too common.
91 TermEnum terms = ir.terms(new Term(field,""));
92 while (terms.next()) {
93 if (!field.equals(terms.term().field())) {
96 int df = terms.docFreq();
98 String ttxt = terms.term().text();
99 pq.insertWithOverflow(new TermDf(ttxt,df));
105 String res[] = new String[pq.size()];
107 while (pq.size()>0) {
108 TermDf tdf = pq.pop();
110 System.out.println(i+". word: "+tdf.df+" "+tdf.word);
115 private static class TermDf {
118 TermDf (String word, int freq) {
124 private static class TermsDfQueue extends PriorityQueue<TermDf> {
125 TermsDfQueue (int maxSize) {
129 protected boolean lessThan(TermDf tf1, TermDf tf2) {
130 return tf1.df < tf2.df;