Querying
Writing a simple query is definitely an easy job; however, writing a complex one with queries playing around with phrases, boosting and prioritizing search results, nesting your query, and a search even based on partial match would be a challenging task. In addition to this, you must remember to write your query taking the performance aspects into account. This is one of the reasons why something that seems to be simple at first sight, actually proves to be even more challenging like writing a complex query which is equally good and efficient in terms of performance. This chapter will guide you through a few of the tasks you are expected to encounter during your everyday work with Solr.
Querying based on a particular field value
You might encounter situations wherein you need to ask for a particular field value, for instance, searching for an author of a book in an internet library or an e-store. Solr can do this for you and we will show you how to achieve it.
Let us assume, we have the following index structure (just add the following lines to the field definition section of your schema.xml
file).
<field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="title" type="text" indexed="true" stored="true" /> <field name="author" type="string" indexed="true" stored="true"/>
Hit the following URL on your browser to ask for a value in the author field, which will send the query to Solr.
http://localhost:8080/solr/select?q=author:surendra
You are done with your search; and the documents you get from Solr will be the ones that have the given value in the author field. Remember that the query shown in the preceding example is using a standard query parser, and not dismax.
We defined three fields in the index (which are just for demonstration purpose, and can be customized based on your requirement). As you can see in the preceding query to ask for a particular field value, you need to send a q
parameter in FIELD_NAME:VALUE
format and that's it. You may extend your search by adding logical operators to the query, hence increasing its complexity.
Tip
In case you forget to specify the field name in your query; your value will be checked again in the default search field that has been defined in the schema.xml
file.
While discussing a particular field value, there are a couple of points you should know and would definitely prove useful for you, which are:
Single value using extended dismax query parser
You may sometimes need to ask for a particular field value when using the dismax query parser. Though the dismax query parser doesn't fully support lucene query syntax; we have an alternative. You can use extended dismax query parser instead. It has the same list of functionality as the dismax query parser and it also fully supports lucene query syntax. The query shown here, but using extended dismax, would look like this:
http://localhost:8080/solr/select?q=author:surendra&defType=edismax
Multiple values in the same field
You may often need to ask for multiple values in a single field. For example, you want to find the solr, monitoring and optimization values in the title field. To do that, you need to run the following query (the brackets surrounding the values are the highlights of this concept):
http://localhost:8080/solr/select?q=author:(solr monitoring optimization)