lucene-多字段、多索引搜索与多线程搜索
程序员文章站
2022-03-02 18:40:55
...
1、多字段搜索
1)
Term t1=new Term("title","会议");
TermQuery q1=new TermQuery(t1);
Term t2=new Term("context","交通部");
TermQuery q2=new TermQuery(t2);
BooleanQuery q=new BooleanQuery();
q.add(q1,BooleanClause.Occur.SHOULD);
q.add(q2,BooleanClause.Occur.SHOULD);
2)
String[] fileds=["title","context"];
String[] keys=["会议","交通部"]
MultiFieldQueryParser mp=new MultiFieldQueryParser(fields,new StandardAnalyzer());
Query q=mp.parse(keys);
还例如:
String[] query = {"query1", "query2", "query3"};
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT};
MultiFieldQueryParser.parse(query, fields, flags, analyzer);
The code above would construct a query:
(filename:query1) +(contents:query2) -(description:query3)
2、多索引
IndexSearcher searcher1=new IndexSearcher("index1");
IndexSearcher searcher2=new IndexSearcher("index2");
IndexSearcher[] searchers={search1,search2};
MultiSearcher searcher=new MultiSearcher(searchers);
3、多线程
IndexSearcher searcher1=new IndexSearcher("index1");
IndexSearcher searcher2=new IndexSearcher("index2");
IndexSearcher[] searchers={search1,search2};
ParallelMultiSearcher searcher=new ParallelMultiSearcher(searchers);