Apache Solr

Solr Faceted Search Example

In this example of Solr faceted search, we will discuss about the use of faceting the data and also discuss different facet options available in Solr. For our discussion, we will be using one of the collection example (techproducts) that comes with the Solr Installation for easy set up. We will show you how to make use of the Solr facet parameters to achieve the desired search results.

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.
 
 
 

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 admin console
Solr admin console

3. Facet Search

Faceting is the process of arranging the search results into categories based on indexed terms. The output of the facet search is the numerical count found for each search term. This feature is very useful in providing better user experience during search by narrowing in on the results.

The following are the general parameters for facet.

  • facet – If set to true, enables faceting.
  • facet.query – Specifies a Lucene query to generate a facet count.

3.1 Field-Value Faceting

In this example we will set the facet value to true and set the facet.field parameter. The facet.field parameter identifies a field to be treated as a facet. The other parameters used in the query are Solr general query parameters and not related to faceting. To get more information on those parameters please look into our previous examples.

Now navigate to the following URL. This will bring the products with price range 0 to 400 and group the results by category.

http://localhost:8983/solr/techproducts/select?q=price:[0 TO 400]&fl=id,name,price&facet=true&facet.field=cat

Solr Facet Field Value
Solr Facet Field Value

There are other handful of facet parameters available to tune the search results when using Field-Value faceting.

3.2 Range Faceting

We can use range faceting on date or numeric fields that support range queries. This feature is very helpful in providing better user experience by bucketing the reference field in ranges. In this example we will use the price field to do the range faceting. The following parameters are used in the query.

  • facet.range – Specifies the field to facet by range.
  • facet.range.start – Specifies the start of the facet range.
  • facet.range.end – Specifies the start of the facet range.
  • facet.range.gap – Specifies the span of the range as a value to be added to the lower bound.

Now navigate to the following URL. This will bring the numerical products with price range bucketed into ranges of 100 for the results.

http://localhost:8983/solr/techproducts/select?q=price:[0 TO 4000]&fl=id,name,price&facet=true&facet.field=cat&facet.range=price&f.price.facet.range.start=0.0&f.price.facet.range.end=1000.0&f.price.facet.range.gap=100

Solr Facet Range
Solr Facet Range

3.3 Interval Faceting

Another feature in Solr is Interval faceting. This looks similar to Range faceting but Interval faceting gives options to set variable range as against the former which can set only a fixed gap.  In order to use Interval Faceting on a field, it is required that the field has “docValues” enabled.

To modify the field lets navigate to example\techproducts\solr\techproducts\conf and set the “docValues” attribute to true in the schema.xml file as shown below.

schema.xml

 <field name="weight" type="float" indexed="true" stored="true"/>
 <field name="price" type="float" indexed="true" stored="true" docValues="true"/>
 <field name="popularity" type="int" indexed="true" stored="true" />
 <field name="inStock" type="boolean" indexed="true" stored="true" />

Since we have modified the configuration we have to restart the Solr instance. Open a command prompt, navigate to bin folder and issue the following commands.

solr stop -all

solr -e techproducts

In this example we will use the following faceting parameters.

  • facet.interval –  Specifies the field to facet by interval.
  • facet.interval.set –  Sets the intervals for the field.

We can use the following syntax to include or exclude the values provided in the set interval.

(1,10) -> will include values greater than 1 and lower than 10.

[1,10) -> will include values greater or equal to 1 and lower than 10.

[1,10] -> will include values greater or equal to 1 and lower or equal to 10.

Now navigate to the following URL. This will bring the numerical count of the products for the intervals provided in the query.

http://localhost:8983/solr/techproducts/select?q=*:*&fl=id,name,price&facet=true&facet.field=cat&facet.interval=price&f.price.facet.interval.set=[0,10]&f.price.facet.interval.set=(10,100]

Solr Facet Interval
Solr Facet Interval

4. Download the Configuration

This was an example of solr faceted search.

Download
You can download the schema for the example here: Schema.xml

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.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tapio
Tapio
6 years ago

dark magic, how the first faceting example knows there exists zero items with category “graphics cards”? Why there isn’t zero items with category “broken hockey sticks”

Back to top button