Search code examples
solrsolrnet

SolrNet : Keep Facet count when filtering query


I receive the following facets when I query :

"Field1": [
  {
    "Key": "Best Facet 1",
    "Value": 999
  },
  {
    "Key": "Best Facet 2",
    "Value": 999
  }
],
"Field2": [
  {
    "Key": "Second Best Facet 1",
    "Value": 421
  },
  {
    "Key": "Second Best Facet 2",
    "Value": 103
  }
]

Now I want to apply filter query (fq) on Field2 = "Second Best Facet 2"

The results I receive are

"Field1": [
  {
    "Key": "Best Facet 1",
    "Value": 103
  },
  {
    "Key": "Best Facet 2",
    "Value": 103
  }
],
"Field2": [
  {
    "Key": "Second Best Facet 1",
    "Value": 103
  },
  {
    "Key": "Second Best Facet 2",
    "Value": 103
  }
]

I need the facet counts to remain consistent and not be affected by the filter applied. I can't find any solutions to the problem.


Solution

  • You can tag your filters and tell Solr to ignore those filters when calculating the facet scores:

    fq={!tag=dt}doctype:pdf&facet.field={!ex=dt}doctype
    

    fq= applies the filter, but tags the filter with the tag dt, and by telling facet.field to exclude any filter tagged as dt, it will still calculate counts across the whole collection and not just the filtered values.

    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.