刚接触全文检索,遇到个问题,对于指定列查询关键字能查到数据,但是在多列上查询则无法查询到数据。
指定列代码查询:
@Test
public void getSimpleField() throws IOException, ParseException{
String keyword = "sayHelloWorldFrom";
Term term = new Term("inboundOperation", keyword);
Query query = new TermQuery(term);
TopDocs topdocs = searcher.search(query, 1000);
printScoreDocs(topdocs.scoreDocs, searcher);
}
private void printScoreDocs(ScoreDoc[] scoreDocs, IndexSearcher searcher)
throws IOException {
System.out.println("一共查询到数据:" +scoreDocs.length);
for (int i = 0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.print("【inboundOperation: " + document.get("inboundOperation")+"】");
System.out.println("【inboundServiceName: " + document.get("inboundServiceName")+"】");
}
}
查询结果如下
一共查询到数据:84
【inboundOperation: sayHelloWorldFrom】【inboundServiceName: ProxyService$test$proxy$test_proxy】
【inboundOperation: sayHelloWorldFrom】【inboundServiceName: ProxyService$test$proxy$test_proxy】
【inboundOperation: sayHelloWorldFrom】【inboundServiceName: ProxyService$test$proxy$test_proxy】
在多列上查询数据的代码:
public void GetMultiField1() throws IOException, ParseException{
String keyword = "sayHelloWorldFrom";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
String[] fields = { "inboundOperation", "inboundServiceName" };
MultiFieldQueryParser mqp =
new MultiFieldQueryParser(Version.LUCENE_40, fields, analyzer);
mqp.setDefaultOperator(Operator.OR);
TopDocs topdocs = searcher.search(mqp.parse(keyword), 1000);
printScoreDocs(topdocs.scoreDocs, searcher);
}
@Test
public void testMutilFiel2() throws ParseException, IOException{
String keyword = "sayHelloWorldFrom";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
String[] fields = { "inboundOperation", "inboundServiceName" };
Query mutilFiledQuery =
MultiFieldQueryParser.parse(Version.LUCENE_40, new String[]{keyword,keyword},
fields, analyzer);
TopDocs topdocs = searcher.search(mutilFiledQuery, 1000);
printScoreDocs(topdocs.scoreDocs, searcher);
}
@Test
public void testMutilFiel3() throws ParseException, IOException{
String keyword = "sayHelloWorldFrom";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
String[] fields = { "inboundOperation", "inboundServiceName" };
Query mutilFiledQuery =
MultiFieldQueryParser.parse(Version.LUCENE_40, keyword, fields,
new Occur[]{Occur.SHOULD,Occur.SHOULD} , analyzer);
TopDocs topdocs = searcher.search(mutilFiledQuery, 1000);
printScoreDocs(topdocs.scoreDocs, searcher);
}
@Test
public void testMutilField3() throws IOException{
BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("sayHelloWorldFrom", "inboundOperation"));
Query query2 = new TermQuery(new Term("sayHelloWorldFrom", "inboundServiceName"));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
TopDocs topdocs = searcher.search(booleanQuery, 1000);
printScoreDocs(topdocs.scoreDocs, searcher);
}
这些方法都不行,实在不清楚什么问题