In this example of Solr query syntax we will discuss about different query formats in Solr. For our discussion, we will be using one of the collection example (techproducts) that comes along with Solr Installation. We will show you, how to use the REST based API’s exposed by Solr and show you how to use various querying parameters .
Our preferred environment for this example is Windows. Before you begin the Solr installation make sure you have JDK installed and
Java_Home is set appropriately.
You may skip the installation and jump directly to the beginning of the example below.
1. Installing Apache Solr
To begin with, lets download the latest version of Apache Solr from the following location:
As of this writing, the stable version available is 5.0.0. Apache Solr has gone through various changes from 4.x.x to 5.0.0, so if you have different version of Solr you need to download the 5.x.x. version to follow this example.
Once the Solr zip file is downloaded unzip it into a folder. The extracted folder will look like the below.
bin folder contains the scripts to start and stop the server. The
example folder contains few example files. We will be using one of them to demonstrate how Solr indexes the data. The
server folder contains the
logs folder where all the Solr logs are written. It will be helpful to check the logs for any error during indexing. The
solr folder under server holds different collection or core. The configuration and data for each of the core/ collection are stored in the respective core/ collection folder.
Apache Solr comes with an inbuilt Jetty server. But before we start the solr instance we must validate the JAVA_HOME is set on the machine.
2. Start Solr Server
Solr provides few useful collection example to learn about the key features. We will use the techproducts collection bundled with Solr for our discussion. To start the Solr server with the techproducts collection let’s open a command prompt, navigate to bin folder and issue the following syntax.
solr -e techproducts
This will start the Solr server under the default port 8983.
We can now open the following URL in the browser and validate that our Solr instance is running. You can also notice the collection techproducts being populated.
3. Solr basic query
Solr provides a simple REST based select query to search on indexed data. We have to provide the context path of the collection (techproducts in our case) and use select in the URL indicating this is a select query. The parameter q is used to specify the search string.
The following query will look for video in all the indexed fields of the techproducts collection. If you notice the video is present in name field of result 1 and present in one of the features for result 2. This type of query can be used for free text searching on documents. Open the following URL in the browser.
4. Solr query parameters
Solr provides a list of parameters that can be used with queries. The below section explains the available parameters and the purpose.
- qt – Query handler for the request. Standard query handler is used if not specified.
- q – It is used to specify the query event.
- fq – Used to specify filter queries.
- sort – Used to sort the results in ascending or descending order.
- start, rows – start specifies the staring number of the result set. By default it is zero. rows specify the number of records to return.
- fl – Used to return selective fields.
- wt – Specifies the response format. Default is XML.
- indent – Setting to true makes the response more readable.
- debugQuery – Setting the parameter to true gives the debugging information as part of response.
- dismax – To specify the dismax parser.
- edismax – To specify the edismax parser.
- facet – Setting to true enables the faceting.
- spatial – Used for geospatial searches.
- spellcheck – Setting to true help in searching similar terms.
5. Solr advanced queries
We can use one or more parameters provided by Solr to construct the query. In this section we will show you few combinations.
5.1. Solr query – selective fields
As we stated earlier, fl parameter can be used to select limited set of fields in the response. This will help to limit the volume of data that pass through system and reduce I/O cost.
We will modify the basic query to return limited set of fields. We have chosen to return id, name and price in the following query.
Open the following URL in the browser. You can notice the result set contains only the selected fields and the size of the response is reduced when measured in bytes.
5.2. Solr query – filter
We can modify the basic query to add filter. In the basic query, we haven’t specified any field to search for the string video and it returned values from name, features etc. But now we will specify where to look for the search string.
Open the following URL in browser. You can notice the result contains only the records that contain video in the name field.
Similarly, we can modify the query to select all the products with category as electronics. Open the following URL in browser. You can notice the result set contains only the electronics products. Also, we have combined the fl parameter to select only id, name and price fields.
5.3. Solr query – faceted Search
Faceting is a special type of search used for arranging the search results into categories. Searches are presented with indexed terms along with count of matching documents. Faceting makes it easy for users to explore search results, narrowing in on exactly the results they are looking for.
Open the following query in browser. You will see at the bottom of the response contains facet_counts for each of the category. Also you can notice we have applied filter on price and selected only specified fields.
http://localhost:8983/solr/techproducts/select?q=price:[0 TO 400]&fl=id,name,price&facet=true&facet.field=cat