1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.util.LuceneTestCase;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.RandomIndexWriter;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.index.TermEnum;
28 import org.apache.lucene.store.Directory;
30 import java.io.IOException;
31 import java.util.LinkedList;
34 * This class tests PhrasePrefixQuery class.
36 public class TestPhrasePrefixQuery extends LuceneTestCase {
41 public void testPhrasePrefix() throws IOException {
42 Directory indexStore = newDirectory();
43 RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
44 Document doc1 = new Document();
45 Document doc2 = new Document();
46 Document doc3 = new Document();
47 Document doc4 = new Document();
48 Document doc5 = new Document();
49 doc1.add(newField("body", "blueberry pie", Field.Store.YES,
50 Field.Index.ANALYZED));
51 doc2.add(newField("body", "blueberry strudel", Field.Store.YES,
52 Field.Index.ANALYZED));
53 doc3.add(newField("body", "blueberry pizza", Field.Store.YES,
54 Field.Index.ANALYZED));
55 doc4.add(newField("body", "blueberry chewing gum", Field.Store.YES,
56 Field.Index.ANALYZED));
57 doc5.add(newField("body", "piccadilly circus", Field.Store.YES,
58 Field.Index.ANALYZED));
59 writer.addDocument(doc1);
60 writer.addDocument(doc2);
61 writer.addDocument(doc3);
62 writer.addDocument(doc4);
63 writer.addDocument(doc5);
64 IndexReader reader = writer.getReader();
67 IndexSearcher searcher = newSearcher(reader);
69 // PhrasePrefixQuery query1 = new PhrasePrefixQuery();
70 MultiPhraseQuery query1 = new MultiPhraseQuery();
71 // PhrasePrefixQuery query2 = new PhrasePrefixQuery();
72 MultiPhraseQuery query2 = new MultiPhraseQuery();
73 query1.add(new Term("body", "blueberry"));
74 query2.add(new Term("body", "strawberry"));
76 LinkedList<Term> termsWithPrefix = new LinkedList<Term>();
78 // this TermEnum gives "piccadilly", "pie" and "pizza".
80 TermEnum te = reader.terms(new Term("body", prefix + "*"));
82 if (te.term().text().startsWith(prefix))
84 termsWithPrefix.add(te.term());
88 query1.add(termsWithPrefix.toArray(new Term[0]));
89 query2.add(termsWithPrefix.toArray(new Term[0]));
92 result = searcher.search(query1, null, 1000).scoreDocs;
93 assertEquals(2, result.length);
95 result = searcher.search(query2, null, 1000).scoreDocs;
96 assertEquals(0, result.length);