Search code examples
solrfacetfaceted-search

Facet groups in Solr - is this possible in one query


I'm working with facets in Solr and I have the concept of facet groups that each contain a number of facets.

Say I have a structure like this

Product Type
- Chairs (50)
- Tables (20)
- Mirrors (5)

Color
- Yellow (5)
- Black (50)
- Red (10)
- Orange (10)

I have an OR relationship between facets within a facet group and an AND relationship between the groups.

So if I choose Chairs as a facet I get 50 products. Using the standard faceting in Solr (and assuming that each product can have exactly one product type and one color) it will now give:

Product Type
- Chairs (50)
- Tables (0)
- Mirrors (0)

Color
- Yellow (5)
- Black (30)
- Red (5)
- Orange (10)

However, what I really want is that the facet counts within Product Type stay the same as that would reflect what would happen if one of them was chosen.

Can this be done with Solr in one query?


Solution

  • This can implemented using tagged filters and then excluding them when creating the facet.

    From the referenced page:

    To implement a multi-select facet for doctype, a GUI may want to still display the other doctype values and their associated counts, as if the doctype:pdf constraint had not yet been applied. Example:

    === Document Type ===
      [ ] Word (42)
      [x] PDF  (96)
      [ ] Excel(11)
      [ ] HTML (63)
    

    To return counts for doctype values that are currently not selected, tag filters that directly constrain doctype, and exclude those filters when faceting on doctype.

    q=mainquery&fq=status:public&fq={!tag=dt}doctype:pdf&facet=on&facet.field={!ex=dt}doctype
    

    Filter exclusion is supported for all types of facets. Both the tag and ex local params may specify multiple values by separating them with commas.