2016-03-15 143 views
0

我试图从某个零售商搜索最匹配的产品(赏金纸巾),我的查询如下,但查询返回0命中。Lucene搜索2字段

BooleanQuery.Builder combine = new BooleanQuery.Builder(); 

Query q1 = new QueryParser("product", new StandardAnalyzer()).parse(QueryParser.escape("product:" + "bounty paper towel")); 
combine.add(q1, BooleanClause.Occur.SHOULD); // find best name match 

Query q2 = new QueryParser("retailer", new StandardAnalyzer()).parse(QueryParser.escape("retailer:" + "Target")); 
combine.add(q2, BooleanClause.Occur.MUST); // Must from this retailer 

searcher.search(combine.build(), hitsPerPage).scoreDocs; 

这有什么错我构建查询的方式吗?

回答

1

你正在逃避你不想逃避的东西。您将字符串“product:bounty纸巾”传递给escape方法,该方法将逃离不想冒险的冒号。实际上,该查询,逃避和分析后,看起来就像这样:

产品:产品\:赏金产品:纸产品:毛巾

你应该逃脱搜索词,而不是整个查询。喜欢的东西:

parser.parse("product:" + QueryParse.escape("bounty paper towels")); 

此外,它看起来像你正在寻找一个短语查询存在,在这种情况下,应该用引号引起来:

parser.parse("product:\"" + QueryParse.escape("bounty paper towels") + "\""); 

您构建布尔查询的样子精细。你可以利用查询语法分析来完成同样的事情,如果你愿意的话,像这样:

parser.parse(
    "product:\"" + QueryParse.escape("bounty paper towels") + "\"" 
    + "+retailer:" + QueryParse.escape("Target") 
); 

但同样,有什么不对BooleanQuery.Builder代替。

1

采用lucene太多年前,但让我试试......

重写你parse部分如下:

... 
Query q1 = new QueryParser("product", new StandardAnalyzer()) 
      .parse("bounty paper towel"); 
... 
Query q2 = new QueryParser("retailer", new StandardAnalyzer()) 
      .parse("Target")); 
... 

所以,你的查询应该只包含目标信息,而不是列名 - 因为它之前已经被引用。