Query nesting
You might come across situations wherein you need to nest a query within another query. Let us imagine that you want to run a query using the standard request handler, but you need to embed a query that is parsed by the dismax query parser inside it. Isn't that interesting? We will show you how to do it.
Let us assume that we use the same field definition in schema.xml
that was used in our previous section "Based on a partial keyword/phrase match".
Our example data looks like this:
<add> <doc> <field name="id">1</field> <field name="title">Reviewed solrcook book</field> </doc> <doc> <field name="id">2</field> <field name="title">Some book reviewed</field> </doc> <doc> <field name="id">3</field> <field name="title">Another reviewed little book</field> </doc> </add>
Here, we are going to use the standard query parser to support lucene query syntax, but we would like to boost phrases using the dismax query parser. At first it seems to be impossible to achieve, but don't worry, we will handle it. Let us suppose that we want to find books having the words, reviewed and book, in their title field; and we would like to boost the reviewed book phrase by 10. Here we go with the query:
http://localhost:8080/solr/select?q=reviewed+AND+book+AND+_ query_:"{!dismax qf=title pf=title^10 v=$qq}"&qq=reviewed+book
The results of the preceding query should look like this:
<?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">2</int> <lst name="params"> <str name="fl">*,score</str> <str name="qq">book reviewed</str> <str name="q">book AND reviewed AND _query_:"{!dismax qf=title pf=title^10 v=$qq}"</str> </lst> </lst> <result name="response" numFound="3" start="0" maxScore="0.77966106"> <doc> <float name="score">0.77966106</float> <str name="id">2</str> <str name="title">Some book reviewed</str> </doc> <doc> <float name="score">0.07087828</float> <str name="id">1</str> <str name="title">Reviewed solrcook book</str> </doc> <doc> <float name="score">0.07087828</float> <str name="id">3</str> <str name="title">Another reviewed little book</str> </doc> </result> </response>
As you can see, we have used the same and simple index, let us skip its description and step into the next section.
Let us focus on the query. The q
parameter is built of two parts connected together with AND operator. The first one reviewed+AND+book
is just a usual query with a logical operator AND
defined. In the second part, building the query starts with a strange looking expression, _query_
. This expression tells Solr that another query should be made that will affect the results list. We then see the expression stating that Solr should use the dismax
query parser (the !dismax
part) along with the parameters that will be passed to the parser (qf
and pf
).
Note
The v
parameter is an abbreviation for value and it is used to pass the value of the q parameter (in our case, reviewed+book
is being passed to the dismax
query parser).
And thats it! We get to the search results which we had expected.