Apache Solr

Solr query syntax examples

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.
 
 
 

Tip
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:

http://lucene.apache.org/solr/downloads.html

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.

solr_folder
Solr folders

The 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.

http://localhost:8983/solr/

solr_query_admin
Solr admin console

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.

http://localhost:8983/solr/techproducts/select?q=video

solr_query_basic
Solr query – basic

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.

http://localhost:8983/solr/techproducts/select?q=video&fl=id,name,price

solr_query_select_field
Solr query – selected fields

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.

http://localhost:8983/solr/techproducts/select?q=name:video

solr_query_filter_1
Solr query – filter name

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.

http://localhost:8983/solr/techproducts/select?q=cat:electronics&fl=id,name,price

solr_query_filter_2
Solr query – filter category

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

solr_query_facet
Solr query – facet

Veeramani Kalyanasundaram

Veera is a Software Architect working in telecom domain with rich experience in Java Middleware Technologies. He is a OOAD practitioner and interested in Performance Engineering.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ann Abraham
Ann Abraham
5 years ago

I tried to query through SOLR admin UI. i get the correct response for the url http://localhost:8983/solr/solr_example/select?q=programingLanguage:shell
ie if I correctly specify the field (programingLanguage)

But the url http://localhost:8983/solr/solr_example/select?q=shell
is not working . Ideally I want to query and get a response in such a way that i need the results wherever the word “shell” appears in the document. Please tell me what went wrong??
OR
PLEASE TELL ME A WAY TO QUERY , IRRESPECTIVE OF THE FIELDS..

Kevin Yang
3 years ago
Reply to  Ann Abraham

q=programingLanguage:shell” is a field query which let Solr search the term “shell” in “programingLanguage” field.
“q=shell” is a term query which let Solr search the default search field.
As far as I know, the default search field of the “/select” request handler is not set in the configsets of this example.You can set it by adding the following line in the solrconfig.xml or if you want to search all the fields, you can consider using “copyField” in Solr.
 

<requestHandler name="/select" class="solr.SearchHandler">
  <lst name="defaults">
        <str name="df">programingLanguage</str>
  </lst>
</requestHandler>
Back to top button