Javatpoint Logo
Javatpoint Logo

Apache Solr Faceting

Faceting is one of Solr's most useful features. When we compare it to traditional databases and NoSQL data stores. The faceted search, which is also known as faceted navigation or faceted browsing, allows users to see a high-level breakdown of their search results, which are running searches based upon one or more aspects of their documents. It allows us to choose filters to drill into those search results.

Faceting refers to the classification of the search results in many different categories.

Types of Faceting

Query faceting - The query faceting returns the number of documents in the current search results that also match the given query.

Date faceting - It provides the number of documents that fall within certain date ranges.

Faceting Query Example

Using the field faceting, we can get the counter for all terms, or we can get just the top terms in any provided field.

For example, let's take the following books.csv file that contains data about different good books.

Using the post tool, we will post this file into Apache Solr.

When we execute the above command, all the documents that are mentioned in the given .csv file will be sent into Solr.

Now, we will execute a faceted query on the field author with rows value 0 on the collection my_core.

Open the web User Interface of Solr and on the left-hand side of the web page, click on the checkbox facet, as shown in the below-given figure.

Apache Solr Faceting

After clicking on the checkbox, we will have three different text fields to pass the parameters of the faceted search. After that, pass the following values as parameters of the query.


Apache Solr Faceting

Finally, execute the query by clicking the Execute Query button.

Apache Solr Faceting

It classified the files in the index based on the author and define the number of books contributed by the authors.

Faceting Using Java Client API

Given is the Java code to add documents to the Solr index. Save this code in a file with the name Hitlighting.java.

The above code will be compiled by executing the following commands in the terminal:

When we compile and run the above command, we will get the below-shown output.

Apache Solr Faceting

Field faceting

The most used form of faceting is field faceting. When we search, the unique values found in a particular field will be requested back along with the number of documents in which they're found. We will visually demonstrate the use of several field facets using the restaurant example. A facet on the restaurant type field, on the state field, and on the city field.

Let's start running our first facet:

The resultant query will appear in this listing

The example above demonstrate the basis for faceting in Solr, i.e., the field faceting on a single-valued field. All the unique value is examined in such faceting with a count of files in which the specified value is retrieved.

Query faceting

Query Faceting can be used to bring back counts for arbitrary subqueries so we get the results that might match a future search and gives analytics that is based upon the number. Apache provides the capability through its implementation of query faceting. The best way to signify this feature is through an example.

The example above demonstrates the most brute-force method for finding search result counts for subqueries in Solr. It runs each subquery as a separate search and seeing how many outputs are found.

Running a query facet to obtain document counts for subqueries.

Different subqueries will be combined in a single request to Apache Solr through the use of query facets. Query facets based upon multiple price ranges

Query

Results

The above example demonstrates the query facets that will be effectively used to create new buckets of information at query time in any Solr query. We could have easily created a new field in Solr, called price_range, which has each of these bucketed values.

Range faceting

Range faceting provides the ability to bucketize the date and the numeric field values into ranges. The range gets returned from Solr as a facet. This can be particularly useful as a replacement for creating many different query facets to represent multiple ranges of values.

Example range facet on the price field

Query

Results

The output of the above example is similar to the result of the previous, with two notable exceptions.

  • Range faceting returns counts for every range that is falling between the facet.range.start.
  • range.end parameters on the query, even those ranges are containing no documents.

Applying filters to your facets

At the most basic level, when we apply filters upon a facet is no more difficult than adding an extra filter (the fq parameter) to our query. If we want to return three facets with our searches-

  • A field facet on the state field.
  • A field facet on the city field.
  • A query facet on the price field.

The initial query and results would look similar to the next listing.

Query

Results


Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA