前缀搜索:
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));
}
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));
}
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));
}
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=(钢|和)铁(是|战)
That"s all!