Solr search query over multiple fields

不打扰是莪最后的温柔 提交于 2019-11-27 12:41:54

问题


Is it possible to search in Solr over two fields using two different words and get back only those results which contain both of them?

For example, if I have fields "type" and "location" , I want only those results who have type='furniture' and location = 'office' in them.


回答1:


You can use boolean operators and search on individual fields.

q=type:furniture AND location:office

If the values are fixed, it is better to use Filter Queries for Performance.

fq=type:furniture AND location:office



回答2:


The suggested solutions have the drawback, that you have to care about escaping special characters. If the user searches for "type:d'or AND location:coffee break" the query will fail.

I suggest to combine two edismax handlers:

 <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>

Call the request handler like this:

http://localhost:8983/solr/collection1/combine?uq1=furniture&uq2=office

Explanation

  • The variables $uq1 and $uq2 will be replaced by the request parameters uq1 and uq2 will.
  • The result of the first edismax query (uq1) is combined by logical AND with the second edismax query (uq2)

Solr Docs

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




回答3:


You can also use the boostQuery function on the dismaxRequest handler as

type=dismax&bq=type:furniture AND location:office



回答4:


fq=type:furniture AND location:office

Instead of using AND, this could be break into two filter queries as well.

fq=type:furniture
fq=location:office


来源:https://stackoverflow.com/questions/8089947/solr-search-query-over-multiple-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!