KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestPhrasePrefixQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.search.IndexSearcher;
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.index.TermEnum;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.IndexWriter;
24 import org.apache.lucene.store.RAMDirectory;
25 import org.apache.lucene.analysis.SimpleAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28
29 import junit.framework.TestCase;
30
31 import java.io.IOException JavaDoc;
32 import java.util.LinkedList JavaDoc;
33
34 /**
35  * This class tests PhrasePrefixQuery class.
36  *
37  * @author Otis Gospodnetic
38  * @version $Id: TestPhrasePrefixQuery.java,v 1.3 2004/03/29 22:48:06 cutting Exp $
39  */

40 public class TestPhrasePrefixQuery
41     extends TestCase
42 {
43     public TestPhrasePrefixQuery(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      *
50      */

51     public void testPhrasePrefix()
52         throws IOException JavaDoc
53     {
54         RAMDirectory indexStore = new RAMDirectory();
55         IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
56         Document doc1 = new Document();
57         Document doc2 = new Document();
58         Document doc3 = new Document();
59         Document doc4 = new Document();
60         Document doc5 = new Document();
61         doc1.add(Field.Text("body", "blueberry pie"));
62         doc2.add(Field.Text("body", "blueberry strudel"));
63         doc3.add(Field.Text("body", "blueberry pizza"));
64         doc4.add(Field.Text("body", "blueberry chewing gum"));
65         doc5.add(Field.Text("body", "piccadilly circus"));
66         writer.addDocument(doc1);
67         writer.addDocument(doc2);
68         writer.addDocument(doc3);
69         writer.addDocument(doc4);
70         writer.addDocument(doc5);
71         writer.optimize();
72         writer.close();
73
74         IndexSearcher searcher = new IndexSearcher(indexStore);
75
76         PhrasePrefixQuery query1 = new PhrasePrefixQuery();
77         PhrasePrefixQuery query2 = new PhrasePrefixQuery();
78         query1.add(new Term("body", "blueberry"));
79         query2.add(new Term("body", "strawberry"));
80
81         LinkedList JavaDoc termsWithPrefix = new LinkedList JavaDoc();
82         IndexReader ir = IndexReader.open(indexStore);
83
84         // this TermEnum gives "piccadilly", "pie" and "pizza".
85
String JavaDoc prefix = "pi";
86         TermEnum te = ir.terms(new Term("body", prefix + "*"));
87         do {
88             if (te.term().text().startsWith(prefix))
89             {
90                 termsWithPrefix.add(te.term());
91             }
92         } while (te.next());
93
94         query1.add((Term[])termsWithPrefix.toArray(new Term[0]));
95         query2.add((Term[])termsWithPrefix.toArray(new Term[0]));
96
97         Hits result;
98         result = searcher.search(query1);
99         assertEquals(2, result.length());
100
101         result = searcher.search(query2);
102         assertEquals(0, result.length());
103     }
104 }
105
Popular Tags