摘要: Solr的查询解析器DisMax Query Parser对于处理一些简单短语很有用,此外,还常常用于跨字段查询,设置不通字段不同的权重。本文整理自Solr官方文档。
DisMax 设计理念
DisMax query parser接口很像Solr的standard
request handler,这使得它能轻松适应大多数应用场景。它有简单的语法规则,很少抛出异常给用户。
DisMax query parser支持一部分Lucene QueryParser语法,在Lucene中,双引号表示短语查询,+/-表示必须出现和不要出现。DisMax查询能自动优化用户的输入,作为一个Solr管理员,你还可以设置boosting
函数,进行boosting
查询,通过一些过滤方法人工干预查询结果。你可以在solrconfig.xml
中指定,也可以在URL中指定来覆盖solrconfig.xml
中的配置。
想知道为什么名字叫DisMax
?其实DisMax
代表的是Maximum Disjunction
,对于DisMax
,有这么一个定义:
A query that generates the union of documents produced by its subqueries, and that scores each document with the maximum score for that document as produced by any subquery, plus a tie breaking increment for any additional matching subqueries.
定义看不懂没关系,你只要知道DisMax
就是为了方便你使用并且有很好的容错能力。
DisMax 参数
除了通用的查询参数,DisMax还有一些自有的查询参数,你可以在solrconfig.xml
中配置这些参数,或者在查询的时候指定,这样会覆盖之前配置的值。
参数 | 描述 |
---|---|
q | 查询参数q |
q.alt | 如果参数q没有指定,就使用standard query parser 并代替参数q |
qf | Query Fields:在哪些字段上去检索数据。如果不指定的话,使用df |
mm | Minimum Match:指定最小匹配因子 |
pf | Phrase Fields:增加文档的得分如果q参数值离的很近 |
ps | Phrase Slop:指定q参数中两个词可以距离多远 |
qs | Query Phrase Slop:指定q参数中两个词可以距离多远,与qf 搭配使用 |
tie | Tie Breaker:使用一个0到1之间的浮点数 |
bq | Boost Query:指定一个额外因素来增加匹配到的文档的得分 |
bf | Boost Functions:指定boosts 使用的函数 |
DisMax Query Parser使用举例
- 使用
StandardRequestHandler
查询”video”
http://localhost:8983/solr/select?q=video&fl=name+score
- 已经配置了查询字段:text、features、name、id、manu、cat。而且匹配上name和cat会有更高的得分
http://localhost:8983/solr/select?defType=dismax&q=video
- 可以将score显示出来,看一下各个文档的得分
http://localhost:8983/solr/select?defType=dismax&q=video&fl=*,score
- 现在想设置features有更高的得分,而text有较低的得分
http://localhost:8983/solr/select?defType=dismax&q=video&qf=features^20.0+text^0.3
- 现在希望某一字段在满足某一情况下有更高的得分
http://localhost:8983/solr/select?defType=dismax&q=video&bq=cat:electronics^5.0
- 现在想使用有另外一个为
instock
的handler,它配置了一个过滤器:inStock:true
http://localhost:8983/solr/select?defType=dismax&q=video&qt=instock&fl=name,score,inStock
- 如果查询短语的单词是一个或者两个,原则上返回的结果中必须包含全部单词,但是你的查询短语很长,solr允许有单词不匹配。你可以通过
mm
参数设置最多有多少单词不匹配。
http://localhost:8983/solr/select?defType=dismax&q=belkin+ipod+gibberish
- 可以通过调试功能验证你的想法。
http://localhost:8983/solr/select?defType=dismax&q=belkin+ipod+gibberish&debugQuery=true