Apache Solr - Querying Data

In addition to storing data, Apache Solr also provides the facility of querying it back as and when required. Solr provides certain parameters using which we can query the data stored in it.

In the following table, we have listed down the various query parameters available in Apache Solr.

Parameter Description
q This is the main query parameter of Apache Solr, documents are scored by their similarity to terms in this parameter.
fq This parameter represents the filter query of Apache Solr the restricts the result set to documents matching this filter.
start The start parameter represents the starting offsets for a page results the default value of this parameter is 0.
rows This parameter represents the number of the documents that are to be retrieved per page. The default value of this parameter is 10.
sort This parameter specifies the list of fields, separated by commas, based on which the results of the query is to be sorted.
fl This parameter specifies the list of the fields to return for each document in the result set.
wt This parameter represents the type of the response writer we wanted to view the result.

You can see all these parameters as options to query Apache Solr. Visit the homepage of Apache Solr. On the left-hand side of the page, click on the option Query. Here, you can see the fields for the parameters of a query.

Query Parameter

Retrieving the Records

Assume we have 3 records in the core named my_core. To retrieve a particular record from the selected core, you need to pass the name and value pairs of the fields of a particular document. For example, if you want to retrieve the record with the value of the field id, you need to pass the name-value pair of the field as − Id:001 as value for the parameter q and execute the query.

Retrieving Records

In the same way, you can retrieve all the records from an index by passing *:* as a value to the parameter q, as shown in the following screenshot.

Retrieve All

Retrieving from the 2nd record

We can retrieve the records from the second record by passing 2 as a value to the parameter start, as shown in the following screenshot.

Next Record

Restricting the Number of Records

You can restrict the number of records by specifying a value in the rows parameter. For example, we can restrict the total number of records in the result of the query to 2 by passing the value 2 into the parameter rows, as shown in the following screenshot.


Response Writer Type

You can get the response in required document type by selecting one from the provided values of the parameter wt.

Response Writer Plan

In the above instance, we have chosen the .csv format to get the response.

List of the Fields

If we want to have particular fields in the resulted documents, we need to pass the list of the required fields, separated by commas, as a value to the property fl.

In the following example, we are trying to retrieve the fields − id, phone, and first_name.

List Fields