How to implement faceting, an enhancement to the display of search results

About Faceting

To make it easier for users to quickly spot the most relevant search results, add faceting functionality. With faceting, search results are grouped under useful headings, using tags you apply ahead of time to the documents in your index. For example, the results of a shopping query for books might be grouped according to the type of book and the price:

NARROW YOUR RESULTS BY:
Most popular
  • Top rated (5)
  • "Very Good" (25)
Content
  • Cookbooks (10)
  • Romance (15)
  • Science Fiction (13)
  • Travel (30)
Price
  • Less than $10 (5)
  • $10-$19.99 (200)
  • $20-$29.99 (140)
  • $30 and up (46)

Each time the user clicks a facet value, the set of results is reduced to only the items that have that value. Additional clicks continue to narrow down the search—the previous facet values are remembered and applied again. For example, the user might click "Cookbooks," and see a list of all 10 cookbook titles next to an updated navigation list like this:

NARROW YOUR RESULTS BY:
Most popular
  • Top rated (1)
  • "Very Good" (3)
Price
  • Less than $10 (1)
  • $10-$19.99 (4)
  • $20-$29.99 (4)
  • $30 and up (1)

If price is more important to the user than the reviews of fellow shoppers, the next click would be on a price range, say "$10-19.99." Now the list might look like this:

NARROW YOUR RESULTS BY:
Most popular
  • "Very Good" (1)

At this point, it is very easy for the user to choose the right cookbook.

Why Use Faceting?

Faceted search results provide an easy-to-scan, browsable display that helps users quickly narrow down each search. The faceting tags that you store with your Searchify documents provide a way to add your own taxonomy to directly control the presentation of search results. In the end, it's about helping the user find the right information. Faceted search gives a user the power to create an individualized navigation path, drilling down through successive refinements to reach the right document. This more effectively mirrors the intuitive thought patterns of most users. Faceted search has become an expected feature, particularly for commerce sites.

How It Works

Faceted search is performed in several parts:

  • Index: to each document in the index, add tags to specify a value for each facet. For example, for each book in the index, tag it with the type of material and the price range.
  • Search results: for every search, the Searchify server returns a count of how many matching documents were tagged with each value within each facet. For example, if the query was for "books," you might find out that in the facet "type of material," your index contains 13 science fiction books, 15 romance novels, and 10 cookbooks; and in the price facet, there are 5 books under $10, 200 books from $10-19.99, and so on.
  • Query: you can include facet values as query criteria. For example, you can write a query that returns only the romance novels under $10.
  • Web page: use the facets and document counts returned by the server to create a set of facet links on your web page, like the example shown above. Then construct queries to be activated by each facet link, passing in the appropriate values—say, querying only for romance novels when the user clicks the "Romance (15)" link.

Quick Start: Copy & Paste

Tweak the following code snippets for your needs, and you're ready to go

Before you start

    • ruby
    • python
    • php
    • java

    For Ruby environments we provide a gem that handles all the REST calls for you in a very Ruby-fashioned way.

    • Download the Ruby client if you have not already done so.
    • Know your index's public URL. You'll need it to instantiate the client. Find the public URL on the Dashboard.

Tag the documents

  • Syntax rules

  • Each facet name is defined as a string, and all values for the facet are also defined as strings.
  • A given document can be tagged with at most one value for each facet. That is, a book can not be of two types.
  • Tips

  • If you like planning ahead, take the time to consider your taxonomy design. You can also simply add facets and values on the fly.
  • The tags applied to documents need not be the same strings that you will display to users. Using internal codes instead of display strings will make it easier to modify your UI later if desired. In our example, we have used display strings to make it easier to follow.

Store the facets and facet values as metadata by adding tags to documents in your index.

The following code shows how to tag a document with several facets at the same time, using the updateCategories() method from the Java client library:

categories = { 
                  'priceRange' => '$10-$19.99',
                  'bookType' => 'cookbook'
             }

index.document(docid).update_categories(categories) 

See Facets in Query Results

After you have tagged documents, the Searchify server will start to show faceting data in the results it returns for search requests. For example, if you search for books about France, you might get results like the following:

{
   'bookType': {
       'cookbook': 2,
       'travel': 4
   },
   'priceRange': {
       'Less than $10': 1,
       '$10-$19.99': 5
   }
}

This shows that there are both cookbooks and travel books related to France, and most of them are between $10 and $20.

Use Facets as Query Criteria

You can filter a search by using facet values. This is similar to using document variables, but uses different syntax. For example, suppose you want to find cookbooks that cost less than $20.00. The following code shows how to do that search by using .withCategoryFilters to include facet values in a query:

index.search(query,
             :category_filters => {
                'priceRange' => ['Less than $10', '$10-$19.99'],
                'bookType' => ['cookBook']
             })

Designing a Faceting Taxonomy

Before you start implementing faceting, take some time to decide on which facets and values make sense for your index. When you consider how to categorize information, all sorts of interesting questions can arise. Depending on the size of your index and whether you are working in a large enterprise, you might need to hold a few meetings involving key people such as website designers, product managers, information architects, and others.

The goal of your design phase is to arrive at a scheme, probably a written list, that defines the facets and their values. For example, suppose you are dealing with the books database from our earlier example. If you decide that one facet is "type of book," what are the book types you want to feature? You'll need to answer questions like these:

  • Will you base the list on which types are most numerous in your index, on which types people most often purchase, or on some other criteria?
  • How many book types do you want to tag before grouping the remaining books into an "Other" category?
  • At what cutoff point does a type of book fall into "Other": if there are fewer than 100 books of that type in the index ... fewer than 10 ... or fewer than 10 purchased in the past quarter?
  • Do you want to keep the book type groups fairly even in size? For example, if you have 100 mystery novels and only a few books of every other type, do you want to break down the "mystery" group into smaller sub-groups, such as "Noir," "Thriller," and "Sherlock Holmes"?

From this example, you can see that a taxonomy can be both complex and dynamic—the design might need to change over time to reflect changes in index contents, business goals, and sales fluctuations.

More Information