LinkedList<Term> termsWithPrefix = new LinkedList<Term>();
IndexReader ir = reader;
// 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(termsWithPrefix.toArray(new Term[0]));
assertEquals("body:\"blueberry (piccadilly pie pizza)\"", query1.toString());
query2.add(termsWithPrefix.toArray(new Term[0]));
assertEquals("body:\"strawberry (piccadilly pie pizza)\"", query2.toString());
ScoreDoc[] result;
result = searcher.search(query1, null, 1000).scoreDocs;
assertEquals(2, result.length);
result = searcher.search(query2, null, 1000).scoreDocs;
assertEquals(0, result.length);
// search for "blue* pizza":
MultiPhraseQuery query3 = new MultiPhraseQuery();
termsWithPrefix.clear();
prefix = "blue";
te = ir.terms(new Term("body", prefix));
do {
if (te.term().text().startsWith(prefix))
{
termsWithPrefix.add(te.term());
}
} while (te.next());
query3.add(termsWithPrefix.toArray(new Term[0]));
query3.add(new Term("body", "pizza"));
result = searcher.search(query3, null, 1000).scoreDocs;
assertEquals(2, result.length); // blueberry pizza, bluebird pizza