1

I am using Solr v8 and am looking for a way to selectively include or exclude zero-count facets based on different fields. I would like to exclude zero-count facets that result from my initial base query but include those that are the result of any additional user-applied filters from the UI.

Here's an example of my Solr index:

+----+----------+----------+-------+
| ID | Category | Material | Color |
+----+----------+----------+-------+
|  1 | Tables   | Wood     | Brown |
|  2 | Tables   | Metal    | Black |
|  3 | Sofas    | Linen    | Beige |
|  4 | Sofas    | Leather  | Brown |
+----+----------+----------+-------+

On browsing the 'Tables' category, I intend to display the facet data as filters, excluding any non-applicable Materials and Colors:

    MATERIAL 
[ ] Wood (1)
[ ] Metal (1)

    COLOR
[ ] Brown (1)
[ ] Black (1)

+----+----------+----------+-------+
|             RESULT               |
| ID | Category | Material | Color |
+----+----------+----------+-------+
|  1 | Tables   | Wood     | Brown |
|  2 | Tables   | Metal    | Black |
+----+----------+----------+-------+

However, on applying additional filters such as Material=Wood, I want to continue displaying zero-count Colors that were excluded due to the new filter but hide those excluded by the category filter:

    MATERIAL 
[X] Wood (1)
[ ] Metal (1)

    COLOR
[ ] Brown (1)
    Black (0) (unselectable)

+----+----------+----------+-------+
|             RESULT               |
| ID | Category | Material | Color |
+----+----------+----------+-------+
|  1 | Tables   | Wood     | Brown |
+----+----------+----------+-------+

(Note: Material is a multi-select filter with tagging.)

This older question didn't provide a clear solution: Offer facets with zero count for certain category with min count set to one using Solr - I tried to facet the Material twice, once excluding the category tag and once without, like so: facet.field={!ex=cat_tag,mat_tag}material&facet.field={!ex=mat_tag}material, but the outputs didn't provide enough information to determine when to hide or show specific zero-count facets.

Is there a way to achieve this, either through a query command or some form of pre/post-processing? Thanks in advance.

Kurt Schindler
  • 21,037
  • 4
  • 43
  • 48
  • 1
    You can set per-field specific faceting parameters (if you have `facet.mincount=1` globally in your example) by using `f.color.facet.mincount=0` - that should give you all those fields that doesn't have a matching document in accordance to the current filters as well. _When_ to include this parameter is the responsibility of your application and wouldn't usually be a good fit to evaluate on the Solr side by itself. – MatsLindh Jul 25 '23 at 12:57
  • Regardless of the global facet mincount, if I set `f.color.facet.mincount=0`, Solr will return all color values across the whole index, exposing the irrelevant ones I want to remain hidden. I'm not seeing how this can be accomplished by conditionally toggling facet `mincount`. – Kurt Schindler Jul 25 '23 at 20:40
  • Yeah, if your goal is to get those that would have been included only if the filter `mat_tag` hadn't been applied, but would be present if `cat_tag` is applied, I think you'll have to implement that yourself - create two sets with the facet values, then add the difference to the second set with a count of 0. – MatsLindh Aug 02 '23 at 11:17

0 Answers0