开发者

Refining Solr searches, getting exact matches?

开发者 https://www.devze.com 2022-12-28 23:06 出处:网络
Afternoon chaps, Right, I\'m constructing a fairly complex (to me anyway) search system for a website using Solr, although this question is quite simple I think...

Afternoon chaps,

Right, I'm constructing a fairly complex (to me anyway) search system for a website using Solr, although this question is quite simple I think...

I have two search criteria, location and type. I want to return results that are exact matches to type (letter to letter, no exceptions), and like location.

My current search query is as follows

../select/?q=location:N1 type:blue&rows=100&fl=*,score&debugQuery=true

This firstly returns all the type blue's that match N1, but then returns any type that matches N1,开发者_开发技巧 which is opposite to what I'm after. Both fields are set as textgen in the Solr schema.

Any pointers?

Cheers gang


By default, Solr uses the OR operator to combine the query terms. If you only want results with location:N1 AND type:blue instead of location:N1 OR type:blue, you'll need to change the operator. The simplest way to change this is by adding an additional parameter q.op=AND to the URL when querying:

../select/?q=location:N1 type:blue&rows=100&fl=*,score&debugQuery=true&q.op=AND

You can also change this for all queries by editing your schema.xml file; look for

<solrQueryParser defaultOperator="OR"/>

and change it to

<solrQueryParser defaultOperator="AND"/>

You might also want to change the field-type of your type field to something that is not tokenized, string for example. This will ensure that the match on that field is exact.

<field name="type" type="string" indexed="true" stored="true"/>


If you only want to have the "type" clause to be mandatory, you can keep the OR as the default operator and use the encoded '+' sign in front of the "type" clause:

./select/?q=location:N1 %2Btype:blue&rows=100&fl=*,score&debugQuery=true
0

精彩评论

暂无评论...
验证码 换一张
取 消