MultiPhraseQuery
实现以下功能:
前缀搜索:
IndexSearcher searcher=new IndexSearcher(INDEX_STORE_PATH);
MultiPhraseQuery query=new MultiPhraseQuery();
query.add(new Term("bookname","钢"));
Term t1=new Term("bookname","铁");
Term t2=new Term("bookname","和");
Term t3=new Term("bookname","要");
query.add(new Term[]{t1,t2,t3});
Hits hits=searcher.search(query);
for(int i=0;i<hits.length();i++)
{
System.out.println(hits.doc(i));
}
后缀搜索:
IndexSearcher searcher=new IndexSearcher(INDEX_STORE_PATH);
MultiPhraseQuery query=new MultiPhraseQuery();
Term t1=new Term("bookname","钢");
Term t2=new Term("bookname","和");
query.add(new Term[]{t1,t2});
query.add(new Term("bookname","铁"));
Hits hits=searcher.search(query);
for(int i=0;i<hits.length();i++)
{
System.out.println(hits.doc(i));
}
混合搜索:
IndexSearcher searcher=new IndexSearcher(INDEX_STORE_PATH);
MultiPhraseQuery query=new MultiPhraseQuery();
Term t1=new Term("bookname","钢");
Term t2=new Term("bookname","和");
query.add(new Term[]{t1,t2});
query.add(new Term("bookname","铁"));
Term t3=new Term("bookname","是");
Term t4=new Term("bookname","战");
query.add(new Term[]{t3,t4});
Hits hits=searcher.search(query);
for(int i=0;i<hits.length();i++)
{
System.out.println(hits.doc(i));
}
可以用正则表达式形象地描绘混合搜索:
query=(钢|和)铁(是|战)
本来,要实现 "a (b|c|d) e" 这样一个语意的Query ,我猜测需要用 "a b e", "a c e" ,"a d e"三个PhraseQuery
来实现, 现在MultiPhraseQuery实现了这样一个方法
public void add(Term[] terms, int position)
也就是说,可以在原来的PhraseQuery中的任意位置添加更多的term
于是也就可以方便的实现"a (b|c|d) e"这样的查询语意了。