Solr搜索多个字段的查询

jay*_*jay 41 solr

是否可以使用两个不同的单词在Solr中搜索两个字段并仅返回包含它们的结果?

例如,如果我有字段"type"和"location",我只想要那些在其中包含type ='furniture'和location ='office'的结果.

Jay*_*dra 64

您可以使用布尔运算符并搜索单个字段.

q=type:furniture AND location:office
Run Code Online (Sandbox Code Playgroud)

如果值已修复,则最好使用"筛选查询性能".

fq=type:furniture AND location:office
Run Code Online (Sandbox Code Playgroud)

  • 每当你有这样一个使用AND的查询时,最好考虑改变它以使用过滤器查询`fq =`因为它可以更快. (3认同)
  • 在投票给答案时请提供意见.当然有帮助. (2认同)

A N*_*ter 5

您还可以在 dismaxRequest 处理程序上使用 boostQuery 函数作为

type=dismax&bq=type:furniture AND location:office
Run Code Online (Sandbox Code Playgroud)


Mat*_*s M 5

建议的解决方案有一个缺点,你必须关心转义特殊字符.如果用户搜索"type:d'或AND location:coffee break",则查询将失败.

我建议结合两个edismax处理程序:

 <requestHandler name="/combine" class="solr.SearchHandler" default="false">
     <lst name="invariants">
       <str name="q">
        (_query_:"{!edismax qf='type' v=$uq1}"
   AND _query_:"{!edismax qf='location' v=$uq2}")
       </str>
     </lst>
  </requestHandler>
Run Code Online (Sandbox Code Playgroud)

像这样调用请求处理程序:

http://localhost:8983/solr/collection1/combine?uq1=furniture&uq2=office
Run Code Online (Sandbox Code Playgroud)

说明

  • 变量$ uq1和$ uq2将被请求参数uq1和uq2替换.
  • 第一个edismax查询(uq1)的结果由逻辑AND与第二个edismax查询(uq2)组合

Solr Docs

https://wiki.apache.org/solr/LocalParams