FileDocCategorySizeDatePackage
TestPrefixFilter.javaAPI DocApache Lucene 2.1.04023Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.search

TestPrefixFilter

public class TestPrefixFilter extends TestCase
Tests {@link PrefixFilter} class.
author
Yura Smolsky
author
yonik

Fields Summary
Constructors Summary
Methods Summary
public voidtestPrefixFilter()

    RAMDirectory directory = new RAMDirectory();

    String[] categories = new String[] {"/Computers/Linux",
                                        "/Computers/Mac/One",
                                        "/Computers/Mac/Two",
                                        "/Computers/Windows"};
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
    for (int i = 0; i < categories.length; i++) {
      Document doc = new Document();
      doc.add(new Field("category", categories[i], Field.Store.YES, Field.Index.UN_TOKENIZED));
      writer.addDocument(doc);
    }
    writer.close();

    // PrefixFilter combined with ConstantScoreQuery
    PrefixFilter filter = new PrefixFilter(new Term("category", "/Computers"));
    Query query = new ConstantScoreQuery(filter);
    IndexSearcher searcher = new IndexSearcher(directory);
    Hits hits = searcher.search(query);
    assertEquals(4, hits.length());

    // test middle of values
    filter = new PrefixFilter(new Term("category", "/Computers/Mac"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(2, hits.length());

    // test start of values
    filter = new PrefixFilter(new Term("category", "/Computers/Linux"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(1, hits.length());

    // test end of values
    filter = new PrefixFilter(new Term("category", "/Computers/Windows"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(1, hits.length());

    // test non-existant
    filter = new PrefixFilter(new Term("category", "/Computers/ObsoleteOS"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(0, hits.length());

    // test non-existant, before values
    filter = new PrefixFilter(new Term("category", "/Computers/AAA"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(0, hits.length());

    // test non-existant, after values
    filter = new PrefixFilter(new Term("category", "/Computers/ZZZ"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(0, hits.length());

    // test zero length prefix
    filter = new PrefixFilter(new Term("category", ""));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(4, hits.length());

    // test non existent field
    filter = new PrefixFilter(new Term("nonexistantfield", "/Computers"));
    query = new ConstantScoreQuery(filter);
    hits = searcher.search(query);
    assertEquals(0, hits.length());