Apache solr Faceting

Faceting in Solr has become the most important feature for enhancing findability and the user search experience for all types of search applications. Faceted search is the dynamic clustering of items and search results into categories that let users refer to the search result. Faceted search allows users to select and apply the filter into those current search results after query. Faceted search returns the index term with counts of how many matching documents are founds for each term.


Faceting types:

    Field Faceting:- Retrieve all terms or just the top terms in a given field. And here field                                        must be indexed.

    Query Faceting:-  Returns the number of documents that match the given query. It is                                         useful to give results for subqueries.
    
    Range Faceting:- Return the number of documents that fall within a certain range.


Implementing Facet:

 As an example for indexing data, going to add books.json file that contains different books data.

[{"id": "978-0641723445","cat": ["book","hardcover"],"name": "The Lightning Thief","author": "Rick Riordan","series_t": "Percy Jackson and the Olympians","sequence_i": 1,"genre_s": "fantasy","inStock": true,"price": 12.5,"pages_i": 384},{"id": "978-1423103349","cat": ["book","paperback"],"name": "The Sea of Monsters","author": "Rick Riordan","series_t": "Percy Jackson and the Olympians","sequence_i": 2,"genre_s": "fantasy","inStock": true,"price": 6.49,"pages_i": 304},{"id": "978-1857995879","cat": ["book","paperback"],"name": "Sophie's World : The Greek Philosophers","author": "Jostein Gaarder","sequence_i": 1,"genre_s": "fantasy","inStock": true,"price": 3.07,"pages_i": 64},{"id": "978-1933988177","cat": ["book","paperback"],"name": "Lucene in Action, Second Edition","author": "Michael McCandless","sequence_i": 1,"genre_s": "IT","inStock": true,"price": 30.5,"pages_i": 475}]



Let's see how to index the above data in solr using post.

./bin/post -c solr_core /home/books.json 

solr_core- solr_core is the name of the core.

After executing the above command all data will be uploaded in solr.

Now let's execute a faceted query on the field author.

Open the web UI then, check the checkbox facet, then pass field name in facet.field text box, as shown in the following screenshot.






Now set  rows=0 and then click the Execute Query.

After clicking Execute Query it will produce the following result.



Parameters are used here:

  • Field: The field name to facet over.


Now let's start JSON faceting.

Like solr faceting, JSON faceting module giver similar functionality but with a stronger emphasis on usability. It has  several benefits like:

-First-class JSON support for metrics and analytics.
-Easier to read nested facet.
-Get a more standardized response format.


Now let's execute a JSON facet query on the field author for the Terms Facet.


Open the web UI then, put this in the Raw Query Parameters

json.facet=
       {       author :
                {    
                       type : terms,  
                       field :author,
                       numBuckets : true, 
                       sort : {count :  desc}
                 } 
}


 as shown in the following screenshot.


Now, execute the Range Facet for the field price as per my index data in solr.

json.facet=
       {       price :
                {    
                       type : range,  
                       field :price,
                       start:1,
                       end:13,
                       gap:5,
                       numBuckets : true, 
                       sort : {count :  desc}
                 } 
}


 as shown in the following screenshot.


Parameters are used here:
  • field: The field name to facet over.
  • sort: Sort the result.
  •  numBuckets: It is boolean. If true, return the number of buckets for the facet. Defaults to false.
  • start: Lower bound of the ranges.
  • end: Upper bound of the ranges.
  • gap: Size of each range bucket produced.