LUCENE-1493: allow setting top number of hits to collect with search.num.hits

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@727063 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-12-16 15:09:46 +00:00
parent 09f661c48c
commit 74e097f8eb
3 changed files with 66 additions and 36 deletions

View File

@ -3,6 +3,9 @@ Lucene Benchmark Contrib Change Log
The Benchmark contrib package contains code for benchmarking Lucene in a variety of ways.
$Id:$
12/16/08
LUCENE-1493: Stop using deprecated Hits API for searching; add new
param search.num.hits to set top N docs to collect.
12/16/08
LUCENE-1492: Added optional readOnly param (default true) to OpenReader task.

View File

@ -539,6 +539,7 @@ Here is a list of currently defined properties:
</li><li>query.maker
</li><li>file.query.maker.file
</li><li>file.query.maker.default.field
</li><li>search.num.hits
</li></ul>
</li>

View File

@ -31,7 +31,8 @@ import org.apache.lucene.benchmark.byTask.feeds.QueryMaker;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
@ -50,7 +51,9 @@ import org.apache.lucene.store.Directory;
* <p/>
* <p>Note: All ReadTasks reuse the reader if it is already open.
* Otherwise a reader is opened at start and closed at the end.
* <p/>
* <p>
* The <code>search.num.hits</code> config parameter sets
* the top number of hits to collect during searching.
* <p>Other side effects: none.
*/
public abstract class ReadTask extends PerfTask {
@ -89,19 +92,23 @@ public abstract class ReadTask extends PerfTask {
QueryMaker queryMaker = getQueryMaker();
Query q = queryMaker.makeQuery();
Sort sort = getSort();
Hits hits;
TopDocs hits;
final int numHits = numHits();
if (numHits > 0) {
if (sort != null) {
hits = searcher.search(q, sort);
hits = searcher.search(q, null, numHits, sort);
} else {
hits = searcher.search(q);
hits = searcher.search(q, numHits);
}
//System.out.println("searched: "+q);
//System.out.println("q=" + q + ":" + hits.totalHits + " total hits");
if (withTraverse()) {
final ScoreDoc[] scoreDocs = hits.scoreDocs;
int traversalSize = Math.min(scoreDocs.length, traversalSize());
if (withTraverse() && hits != null) {
int traversalSize = Math.min(hits.length(), traversalSize());
if (traversalSize > 0) {
boolean retrieve = withRetrieve();
int numHighlight = Math.min(numToHighlight(), hits.length());
int numHighlight = Math.min(numToHighlight(), scoreDocs.length);
Analyzer analyzer = getRunData().getAnalyzer();
Highlighter highlighter = null;
int maxFrags = 1;
@ -111,7 +118,7 @@ public abstract class ReadTask extends PerfTask {
}
boolean merge = isMergeContiguousFragments();
for (int m = 0; m < traversalSize; m++) {
int id = hits.id(m);
int id = scoreDocs[m].doc;
res++;
if (retrieve) {
Document document = retrieveDoc(ir, id);
@ -129,6 +136,7 @@ public abstract class ReadTask extends PerfTask {
}
}
}
}
searcher.close();
}
@ -178,6 +186,24 @@ public abstract class ReadTask extends PerfTask {
return Integer.MAX_VALUE;
}
static final int DEFAULT_SEARCH_NUM_HITS = 10;
private int numHits;
public void setup() throws Exception {
super.setup();
numHits = getRunData().getConfig().get("search.num.hits", DEFAULT_SEARCH_NUM_HITS);
}
/**
* Specify the number of hits to retrieve. Tasks should override this if they want to restrict the number
* of hits that are collected during searching. Must be greater than 0.
*
* @return 10 by default, or search.num.hits config if set.
*/
public int numHits() {
return numHits;
}
/**
* Return true if, with search & results traversing, docs should be retrieved.
*/