Search and filter facet data

I’d like to set up a facet search such that I can filter on one or more facets, then get search results that are only associated with the selected facets.

For example, if these are my records:

{
  id: 1,
  shelves: ['us-history', 'fiction', 'revolutionary-war']
},
{
  id: 2,
  shelves: ['us-history', 'revolutionary-war', 'founding-documents']
},
{
  id: 3,
  shelves: ['fiction', 'sailing']
}

Initially I expect to see a list of every distinct facet here. If I filter on us-history, the remaining search results should be ['fiction', 'revolutionary-war', 'founding-documents'] with sailing removed from the list because there are no records that contain both sailing and us-history in the same array.

I’ve found the documentation for searchForFacetValues, so I know I can search facets and get counts, but I don’t see how to filter the remaining facets based on a selected facet. Is this possible?

Hi @jmc,

In Algolia terms, shelves is the facet, and us-history is the facet value. The records are associated to each value and refining on a facet is additive, meaning that selecting sailing and us-history expresses the desire to see the list of all hits that match either value (sailing or us-history).

Algolia doesn’t have a concept of the overlap of records between values, and would not remove a facet value just because none of the current hits contain it, because of this additive property.

If you’d like to use these values to further refine the results, I’d recommend splitting the values across multiple attributes. For instance, an attribute for fiction/non-fiction alongside your shelves attribute would allow you to search for only records that are both non-fiction and about sailing.

You could also do something more complex by using filters, but you’d need to create your own custom components to manipulate the filter yourself.

1 Like