Faceted search with SOLR

Source: Internet
Author: User
Tags solr query

Faceted search has become a critical feature for enhancing findability and the user search experience for all types of search applications. In this article, SOLR creator yonik Seeley gives an introduction to Faceted search with SOLR.

By yonik Seeley

What is Faceted search?

Faceted search is the dynamic clustering of items or search results into categories that let users drill into search results (or even skip searching entirely) by any value in any field. each facet displayed also shows the number of hits within the search that match that category. users can then "drill down" by applying specific contstraints to the search results. faceted search is also called faceted browsing, faceted navigation, guided navigation and sometimes parametric search.

It's easiest to understand what Faceted search is through an example, appropriately from cnet reviews, the first website to use SOLR even before it had been contributed to Apache by CNET.

This example is actually faceted browsing because it started with all digital cameras and not a user search.ManufacturerIs a facet of these search results, and the facet constraints or facet values for this facet includeCanon USA,Sony, AndNikon. A previous screen also encoded DEDPriceAndDigital Camera typeFacets, but they are no longer displayed because we already selected$400-$500AndSLRConstraints from those facets. due to those constraints, the resulting facet counts and camera listings reflect only digital SLR cameras between $400 and $500 in price. any of the displayed facet constraints can be clicked to drill down and further narrow the search results by that constraint. applied constraints can be removed by clicking on them within the breadcrumb trail.

Faceted search provides an alternative tive way to allow users to refine search results, continually drilling down until the desired items are found. The benefits include

  • Superior feedback-users can see at a glance a Summary of the search results and how those results break down by different criteria.
  • No surprises or dead ends-users know how many results match before they click. values with zero counts are normally removed to reduce visual noise and eliminate the possibility of a user accidentally selecting a constraint that wowould lead to no results.
  • No selection hierarchy is imposed-users are generally free to add or remove constraints in any order.


Implementing faceting with SOLR

It's relatively simple to get faceting information from SOLR, as there are few prerequisites. SOLR offers the following types of faceting, all of which can be requested with no prior Configuration:

  • Field faceting-retrieve the counts for all terms, or just the top terms in any given field. The field must be indexed.
  • Query faceting-return the number of statements in the current search results that also match the given query.
  • Date faceting-return the number of arguments that fall within certain date ranges.

Faceting commands are added to any normal SOLR query request, and the faceting counts come back in the same query response. if you are not familiar with the details of making requests in SOLR, see the tutorial.

Implementing the manufacturer facet

To implementManufacturerFacet, I send a field faceting command to SOLR. this example assumes a field named "Manu" exists in the schema and that it has the manufacturer indexed as a single token. the "string" type in the SOLR schema is an appropriate field type to meet these needs.

Let's assume for a moment that the user typed "camera" into the search box. The SOLR query to retrieve the top "camera" matches woshould be:



To retrieve facet counts for the "Manu" field, we wowould simply add the following parameters to that query request:

&facet=true &facet.field=manu

Any number of facet commands can be added to a single query request. To facet on both the "Manu" field and the "camera_type" field, we wocould Add the following parameters:

&facet=true &facet.field=manu &facet.field=camera_type


The query response will now contain facet count information for the given fields in addition to the top matches for the query.

"facet_fields" : {   "manu" : [     "Canon USA" , 25,     "Olympus" , 21,     "Sony" , 12,     "Panasonic" , 9,     "Nikon" , 4 ],   "camera_type" : [     "Compact" , 17,     "Ultracompact" , 11,     "SLR" , 9,     "Full body" , 8 ] } 

Facet counts returned are always in the context of the current query. for example, there may be 100 cameras manufacturerd by Canon in the search index, but only 17 that match the current search results. now it's up to the presentation layer to display this information to the user in the form of clickable links with counts displayed next to them.

Implementing the price facet

If we request field faceting on the "price" field, we get back counts for individual prices. however, we want price ranges, not individual prices. one workaround is to index another field that contains the ranges that the prices fall into (for example 100_200, 200_300, 300_400) and use field faceting on that field. A more flexible solution is to utilize query facets that provide the ability to retrieve facet counts for arbitrary queries.

Let's assume that we have an indexed "price" field and we want to get the facet counts for the following ranges of prices: $100 or less, $100-$200, $200-$300, $300-$400, $400-$500, and $500 or over. we simply add a facet. query command to our query request for each desired range:

&facet=true     &facet.query=price:[* TO 100]     &facet.query=price:[100 TO 200]     &facet.query=[price:200 TO 300]     &facet.query=price:[300 TO 400]     &facet.query=[price:400 TO 500]     &facet.query=price:[500 TO *]

In addition to the standard query results and any field faceting counts requested, the query response will also contain a facet count for each facet query that was specified.

"facet_queries" : {   "price:[* TO 100]" : 28,   "price:[100 TO 200]" : 54,   "price:[200 TO 300]" : 98,   "price:[300 TO 400]" : 84,   "price:[400 TO 500]": 73,   "price:[500 TO *]" : 56 } 


Applying Constraints

Now that we're 've learned how to retrieve facet counts, how do we allow the user to drill down and narrow the search results with any of those constraints? The answer is standard SOLR filter queries, where search results are filtered by any number of arbitrary queries.

Let's assume again that the user typed "camera" into the search box and we have queried SOLR for the top matching events, and also requested some facet counts to be displayed to the user.

http://localhost:8983/solr/query?q=camera   &facet=on   &facet.field=manu   &facet.field=camera_type   &facet.query=price:[* TO 100]   &facet.query=price:[100 TO 200]   &facet.query=[price:200 TO 300]   &facet.query=price:[300 TO 400]   &facet.query=[price:400 TO 500]   &facet.query=price:[500 TO *]

Now let's assume that the user wants to drill down on the constraint $400-$500 from the price facet to get a new set of results that include only cameras in that price range. for this we useFQ(Filter query) parameter, which allows one to filter by a query. We'll also send the relevant faceting commands again since we also want to update the facet counts.

http://localhost:8983/solr/query?q=camera   &facet=on&facet.field=manu&facet.field=camera_type   &fq=price:[400 to 500]

TheFQCommand can appear anywhere in the query request-Parameter order does not matter. notice that we no longer request the facet counts for the prices since we are constraining results to one price range and thus already know that the counts for other ranges wowould be zero.

Now that we 've ve got the SOLR response and updated the facet display and results listing in our web frontend (including adding "$400-$500" breadcrumb to allow the user to remove it if desired ), let's assume that the user clicks on "SLR" in the camera_type facet. to filter our search results further, we'll simply add an additionalFQParameter.

http://localhost:8983/solr/query?q=camera   &facet=on&facet.field=manu   &fq=price:[400 to 500]   &fq=camera_type:SLR

When the SOLR response to this request is rejected ed by the Web Front-end, we can add an "SLR" breadcrumb, and once again update our facet counts and top search results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.