FileDocCategorySizeDatePackage
TestFilteredQuery.javaAPI DocApache Lucene 1.94227Mon Feb 20 09:19:32 GMT 2006org.apache.lucene.search

TestFilteredQuery

public class TestFilteredQuery extends TestCase
FilteredQuery JUnit tests.

Created: Apr 21, 2004 1:21:46 PM

author
Tim Jones
version
$Id: TestFilteredQuery.java 150585 2004-10-10 15:44:45Z dnaber $
since
1.4

Fields Summary
private IndexSearcher
searcher
private RAMDirectory
directory
private Query
query
private Filter
filter
Constructors Summary
Methods Summary
public voidsetUp()

    directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter (directory, new WhitespaceAnalyzer(), true);

    Document doc = new Document();
    doc.add (new Field("field", "one two three four five", Field.Store.YES, Field.Index.TOKENIZED));
    doc.add (new Field("sorter", "b", Field.Store.YES, Field.Index.TOKENIZED));
    writer.addDocument (doc);

    doc = new Document();
    doc.add (new Field("field", "one two three four", Field.Store.YES, Field.Index.TOKENIZED));
    doc.add (new Field("sorter", "d", Field.Store.YES, Field.Index.TOKENIZED));
    writer.addDocument (doc);

    doc = new Document();
    doc.add (new Field("field", "one two three y", Field.Store.YES, Field.Index.TOKENIZED));
    doc.add (new Field("sorter", "a", Field.Store.YES, Field.Index.TOKENIZED));
    writer.addDocument (doc);

    doc = new Document();
    doc.add (new Field("field", "one two x", Field.Store.YES, Field.Index.TOKENIZED));
    doc.add (new Field("sorter", "c", Field.Store.YES, Field.Index.TOKENIZED));
    writer.addDocument (doc);

    writer.optimize ();
    writer.close ();

    searcher = new IndexSearcher (directory);
    query = new TermQuery (new Term ("field", "three"));
    filter = new Filter() {
      public BitSet bits (IndexReader reader) {
        BitSet bitset = new BitSet(5);
        bitset.set (1);
        bitset.set (3);
        return bitset;
      }
    };
  
public voidtearDown()

    searcher.close();
    directory.close();
  
public voidtestFilteredQuery()

    Query filteredquery = new FilteredQuery (query, filter);
    Hits hits = searcher.search (filteredquery);
    assertEquals (1, hits.length());
    assertEquals (1, hits.id(0));

    hits = searcher.search (filteredquery, new Sort("sorter"));
    assertEquals (1, hits.length());
    assertEquals (1, hits.id(0));

    filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "one")), filter);
    hits = searcher.search (filteredquery);
    assertEquals (2, hits.length());

    filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "x")), filter);
    hits = searcher.search (filteredquery);
    assertEquals (1, hits.length());
    assertEquals (3, hits.id(0));

    filteredquery = new FilteredQuery (new TermQuery (new Term ("field", "y")), filter);
    hits = searcher.search (filteredquery);
    assertEquals (0, hits.length());
  
public voidtestRangeQuery()
This tests FilteredQuery's rewrite correctness

    RangeQuery rq = new RangeQuery(
        new Term("sorter", "b"), new Term("sorter", "d"), true);

    Query filteredquery = new FilteredQuery(rq, filter);
    Hits hits = searcher.search(filteredquery);
    assertEquals(2, hits.length());