views:

65

answers:

2

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

+3  A: 

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"/>
Siddhartha Reddy
Cheers buddy, some very useful tips, much appreciated :)
thebluefox
+2  A: 

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
Pascal Dimassimo
Worked wonderfully, thanks Pascal!
thebluefox