FileDocCategorySizeDatePackage
TestPhrasePrefixQuery.javaAPI DocApache Lucene 1.4.33450Tue Mar 30 00:48:06 BST 2004org.apache.lucene.search

TestPhrasePrefixQuery

public class TestPhrasePrefixQuery extends TestCase
This class tests PhrasePrefixQuery class.
author
Otis Gospodnetic
version
$Id: TestPhrasePrefixQuery.java,v 1.3 2004/03/29 22:48:06 cutting Exp $

Fields Summary
Constructors Summary
public TestPhrasePrefixQuery(String name)

        super(name);
    
Methods Summary
public voidtestPhrasePrefix()

        RAMDirectory indexStore = new RAMDirectory();
        IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
        Document doc1 = new Document();
        Document doc2 = new Document();
        Document doc3 = new Document();
        Document doc4 = new Document();
        Document doc5 = new Document();
        doc1.add(Field.Text("body", "blueberry pie"));
        doc2.add(Field.Text("body", "blueberry strudel"));
        doc3.add(Field.Text("body", "blueberry pizza"));
        doc4.add(Field.Text("body", "blueberry chewing gum"));
        doc5.add(Field.Text("body", "piccadilly circus"));
        writer.addDocument(doc1);
        writer.addDocument(doc2);
        writer.addDocument(doc3);
        writer.addDocument(doc4);
        writer.addDocument(doc5);
        writer.optimize();
        writer.close();

        IndexSearcher searcher = new IndexSearcher(indexStore);

        PhrasePrefixQuery query1 = new PhrasePrefixQuery();
        PhrasePrefixQuery query2 = new PhrasePrefixQuery();
        query1.add(new Term("body", "blueberry"));
        query2.add(new Term("body", "strawberry"));

        LinkedList termsWithPrefix = new LinkedList();
        IndexReader ir = IndexReader.open(indexStore);

        // this TermEnum gives "piccadilly", "pie" and "pizza".
        String prefix = "pi";
        TermEnum te = ir.terms(new Term("body", prefix + "*"));
        do {
            if (te.term().text().startsWith(prefix))
            {
                termsWithPrefix.add(te.term());
            }
        } while (te.next());

        query1.add((Term[])termsWithPrefix.toArray(new Term[0]));
        query2.add((Term[])termsWithPrefix.toArray(new Term[0]));

        Hits result;
        result = searcher.search(query1);
        assertEquals(2, result.length());

        result = searcher.search(query2);
        assertEquals(0, result.length());