FileDocCategorySizeDatePackage
TestLazyProxSkipping.javaAPI DocApache Lucene 2.1.05370Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.index

TestLazyProxSkipping

public class TestLazyProxSkipping extends TestCase
Tests lazy skipping on the proximity file.

Fields Summary
private Searcher
searcher
private int
seeksCounter
private String
field
private String
term1
private String
term2
private String
term3
Constructors Summary
Methods Summary
private voidcreateIndex(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 voidperformTest(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.Hitssearch()

        // 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 voidtestLazySkipping()

        // test whether only the minimum amount of seeks() are performed
        performTest(5);
        performTest(10);