Search code examples
solrsolrj

How do I use a different query for a facet count


Given a search for q=*:*&facet=true&facet.field=category solr returns something like:

jackets (10)
shirts (5)

If I change the query to q=name:shirt&facet=true&facet.field=category then I get

jackets (0)
shirts (5)

I want to use a different query to generate the facets so it will count all facets not just those matching the query i.e. for a search of "q=name:shirt" it should return

jackets (10)
shirts (5)

I tried adding a facet.query parameter but it seems to have no effect. facet.query=*:* still returns

jackets (0)
shirts (5)

if I use a query of q=name:shirt

How do I use a different query to generate the facets? I'm using solr 5.5.0


Solution

  • So it seems facet.query is a facet query within the query search results. The solution is to use tags and exclusions e.g.

    q={!tag=name}name&facet=true&facet.field={!ex=name}category
    

    So for each query that I want to exclude I tag it and then reference it in the ex section of the facet.field

    Although I'm starting to think that it may be better to use two queries and simply cache the facet results on the app server ...