Methods Summary |
---|
private void | createIndex(int numHits)
int numDocs = 500;
Directory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
String content;
if (i % (numDocs / numHits) == 0) {
// add a document that matches the query "term1 term2"
content = this.term1 + " " + this.term2;
} else if (i % 15 == 0) {
// add a document that only contains term1
content = this.term1 + " " + this.term1;
} else {
// add a document that contains term2 but not term 1
content = this.term3 + " " + this.term2;
}
doc.add(new Field(this.field, content, Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
}
// make sure the index has only a single segment
writer.optimize();
writer.close();
// the index is a single segment, thus IndexReader.open() returns an instance of SegmentReader
SegmentReader reader = (SegmentReader) IndexReader.open(directory);
// we decorate the proxStream with a wrapper class that allows to count the number of calls of seek()
reader.proxStream = new SeeksCountingStream(reader.proxStream);
this.searcher = new IndexSearcher(reader);
|
private void | performTest(int numHits)
createIndex(numHits);
this.seeksCounter = 0;
Hits hits = search();
// verify that the right number of docs was found
assertEquals(numHits, hits.length());
// check if the number of calls of seek() does not exceed the number of hits
assertEquals(numHits, this.seeksCounter);
|
private org.apache.lucene.search.Hits | search()
// create PhraseQuery "term1 term2" and search
PhraseQuery pq = new PhraseQuery();
pq.add(new Term(this.field, this.term1));
pq.add(new Term(this.field, this.term2));
return this.searcher.search(pq);
|
public void | testLazySkipping()
// test whether only the minimum amount of seeks() are performed
performTest(5);
performTest(10);
|