| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 21 |
|
import org.apache.lucene.document.Document; |
| 22 |
|
import org.apache.lucene.document.Field; |
| 23 |
|
import org.apache.lucene.index.IndexReader; |
| 24 |
|
import org.apache.lucene.index.IndexWriter; |
| 25 |
|
import org.apache.lucene.index.Term; |
| 26 |
|
import org.apache.lucene.index.TermEnum; |
| 27 |
|
import org.apache.lucene.store.RAMDirectory; |
| 28 |
|
|
| 29 |
|
import java.io.IOException; |
| 30 |
|
import java.util.LinkedList; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
@author |
| 36 |
|
@version |
| 37 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (45) |
Complexity: 4 |
Complexity Density: 0.1 |
|
| 38 |
|
public class TestPhrasePrefixQuery |
| 39 |
|
extends TestCase |
| 40 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 41 |
1
|
public TestPhrasePrefixQuery(String name)... |
| 42 |
|
{ |
| 43 |
1
|
super(name); |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (42) |
Complexity: 3 |
Complexity Density: 0.08 |
1
PASS
|
|
| 49 |
1
|
public void testPhrasePrefix()... |
| 50 |
|
throws IOException |
| 51 |
|
{ |
| 52 |
1
|
RAMDirectory indexStore = new RAMDirectory(); |
| 53 |
1
|
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); |
| 54 |
1
|
Document doc1 = new Document(); |
| 55 |
1
|
Document doc2 = new Document(); |
| 56 |
1
|
Document doc3 = new Document(); |
| 57 |
1
|
Document doc4 = new Document(); |
| 58 |
1
|
Document doc5 = new Document(); |
| 59 |
1
|
doc1.add(new Field("body", "blueberry pie", Field.Store.YES, Field.Index.TOKENIZED)); |
| 60 |
1
|
doc2.add(new Field("body", "blueberry strudel", Field.Store.YES, Field.Index.TOKENIZED)); |
| 61 |
1
|
doc3.add(new Field("body", "blueberry pizza", Field.Store.YES, Field.Index.TOKENIZED)); |
| 62 |
1
|
doc4.add(new Field("body", "blueberry chewing gum", Field.Store.YES, Field.Index.TOKENIZED)); |
| 63 |
1
|
doc5.add(new Field("body", "piccadilly circus", Field.Store.YES, Field.Index.TOKENIZED)); |
| 64 |
1
|
writer.addDocument(doc1); |
| 65 |
1
|
writer.addDocument(doc2); |
| 66 |
1
|
writer.addDocument(doc3); |
| 67 |
1
|
writer.addDocument(doc4); |
| 68 |
1
|
writer.addDocument(doc5); |
| 69 |
1
|
writer.optimize(); |
| 70 |
1
|
writer.close(); |
| 71 |
|
|
| 72 |
1
|
IndexSearcher searcher = new IndexSearcher(indexStore); |
| 73 |
|
|
| 74 |
|
|
| 75 |
1
|
MultiPhraseQuery query1 = new MultiPhraseQuery(); |
| 76 |
|
|
| 77 |
1
|
MultiPhraseQuery query2 = new MultiPhraseQuery(); |
| 78 |
1
|
query1.add(new Term("body", "blueberry")); |
| 79 |
1
|
query2.add(new Term("body", "strawberry")); |
| 80 |
|
|
| 81 |
1
|
LinkedList termsWithPrefix = new LinkedList(); |
| 82 |
1
|
IndexReader ir = IndexReader.open(indexStore); |
| 83 |
|
|
| 84 |
|
|
| 85 |
1
|
String prefix = "pi"; |
| 86 |
1
|
TermEnum te = ir.terms(new Term("body", prefix + "*")); |
| 87 |
1
|
do { |
| 88 |
4
|
if (te.term().text().startsWith(prefix)) |
| 89 |
|
{ |
| 90 |
3
|
termsWithPrefix.add(te.term()); |
| 91 |
|
} |
| 92 |
4
|
} while (te.next()); |
| 93 |
|
|
| 94 |
1
|
query1.add((Term[])termsWithPrefix.toArray(new Term[0])); |
| 95 |
1
|
query2.add((Term[])termsWithPrefix.toArray(new Term[0])); |
| 96 |
|
|
| 97 |
1
|
Hits result; |
| 98 |
1
|
result = searcher.search(query1); |
| 99 |
1
|
assertEquals(2, result.length()); |
| 100 |
|
|
| 101 |
1
|
result = searcher.search(query2); |
| 102 |
1
|
assertEquals(0, result.length()); |
| 103 |
|
} |
| 104 |
|
} |