以前的搜索都是单域搜索,下面就是多域搜索MultiFieldQueryParser
public static Query parse(String[ ] queries,String[ ] fields,BooleanClause.Occur.[ ] flags,Analyzer analyzer) t hroiws ParseExcepton
queries 表示要查找的域中关键字;fields表示域的名称;flags表示查询子句间的关系;analyzer表示分析器。
索引并非总存放在一个目录中,很多时候是按照“某种散列算法”放置在不同的目录中;这就用到了
MultiSearcher:
第一个索引
package
ch4.searcher;
import
java.io.IOException;
import
org.apache.lucene.analysis.standard.
*
;
import
org.apache.lucene.document.
*
;
import
org.apache.lucene.index.
*
;
import
org.apache.lucene.search.
*
;
import
org.apache.lucene.search.spans.
*
;
import
org.apache.lucene.queryParser.ParseException;
import
org.apache.lucene.queryParser.
*
;
public
class
Multisearcher
{
private
static
String INDEX_STORE_PATH1
=
"
d:/ch4/TermQuery10
"
;
private
static
String INDEX_STORE_PATH2
=
"
d:/ch4/TermQuery11
"
;
public
static
void
main(String[] args)
throws
Exception
{
Multisearcher.multisearcher();
}
public
static
void
multisearcher()
throws
Exception
{
IndexWriter writer
=
new
IndexWriter(INDEX_STORE_PATH1,
new
StandardAnalyzer(),
true
);
writer.setUseCompoundFile(
false
);
Document doc1
=
new
Document();
Field f1
=
new
Field(
"
bookname
"
,
"
钢铁是怎样炼成的
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f11
=
new
Field(
"
price
"
,
"
20.5
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc1.add(f1);
doc1.add(f11);
Document doc2
=
new
Document();
Field f2
=
new
Field(
"
bookname
"
,
"
钢铁战士
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f22
=
new
Field(
"
price
"
,
"
18.4
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc2.add(f2);
doc2.add(f22);
Document doc3
=
new
Document();
Field f3
=
new
Field(
"
bookname
"
,
"
钢和铁是两种不同的元素
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f33
=
new
Field(
"
price
"
,
"
7.6
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc3.add(f3);
doc3.add(f33);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
第二个索引
//
创建第二个索引器;
IndexWriter writer2
=
new
IndexWriter(INDEX_STORE_PATH2,
new
StandardAnalyzer(),
true
);
writer.setUseCompoundFile(
false
);
Document doc4
=
new
Document();
Field f4
=
new
Field(
"
bookname
"
,
"
钢要比铁有更多的元素
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f44
=
new
Field(
"
price
"
,
"
22.5
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc4.add(f4);
doc4.add(f44);
Document doc5
=
new
Document();
Field f5
=
new
Field(
"
bookname
"
,
"
钢和铁是两种重要的金属
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f55
=
new
Field(
"
price
"
,
"
15.9
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc5.add(f5);
doc5.add(f55);
Document doc6
=
new
Document();
Field f6
=
new
Field(
"
bookname
"
,
"
钢铁是两种重要的金属
"
,Field.Store.YES,Field.Index.TOKENIZED);
Field f66
=
new
Field(
"
price
"
,
"
19.00
"
,Field.Store.YES,Field.Index.UN_TOKENIZED);
doc6.add(f6);
doc6.add(f66);
writer.addDocument(doc4);
writer.addDocument(doc5);
writer.addDocument(doc6);
writer2.close();
多域搜索
String query1
=
"
钢
"
;
String query2
=
"
[10 TO 20]
"
;
//
注意格式:中括号还有关键字TO是大写的
String[] queries
=
{query1,query2};
//
指定两个域Field
String field1
=
"
bookname
"
;
String field2
=
"
price
"
;
String[] fields
=
{field1,field2};
//
指定查询字句之间的关系
BooleanClause.Occur[] clauses
=
{BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST};
//
转成多域查询MultiFieldQuery
Query q
=
MultiFieldQueryParser.parse(queries, fields, clauses,
new
StandardAnalyzer() );
//
打印Query的内容
System.out.println(q.toString());
//
创建两个IndexSearcher,以实现在多个索引目录进行查询
IndexSearcher searcher1
=
new
IndexSearcher(INDEX_STORE_PATH1);
IndexSearcher searcher2
=
new
IndexSearcher(INDEX_STORE_PATH2);
IndexSearcher[] searchers
=
{searcher1,searcher2};
//
使用MultiSearcher进行多域搜索
MultiSearcher searcher
=
new
MultiSearcher(searchers);
Hits hits
=
searcher.search(q);
for
(
int
i
=
0
;i
<
hits.length();i
++
)
{
System.out.println(hits.doc(i));
}
}
}
MultiSearcher原理是对一个IndexSearcher的数组进行循环遍历。分别进行查找,然后合并,使用HitCollector收集后返回;
注:1,其实这个程序还没调试好……
2.打印Query内容是可以直接写成System.out.println(q).