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

TestPrefixQuery

public class TestPrefixQuery extends TestCase
Tests {@link PrefixQuery} class.
author
Erik Hatcher

Fields Summary
Constructors Summary
Methods Summary
public voidtestPrefixQuery()

    RAMDirectory directory = new RAMDirectory();

    String[] categories = new String[] {"/Computers",
                                        "/Computers/Mac",
                                        "/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();

    PrefixQuery query = new PrefixQuery(new Term("category", "/Computers"));
    IndexSearcher searcher = new IndexSearcher(directory);
    Hits hits = searcher.search(query);
    assertEquals("All documents in /Computers category and below", 3, hits.length());

    query = new PrefixQuery(new Term("category", "/Computers/Mac"));
    hits = searcher.search(query);
    assertEquals("One in /Computers/Mac", 1, hits.length());