Filter irrelevant facets from Solr results

I've spent hours poking around for a solution to my problem. I've seen people ask questions close to what I'm trying to accomplish, but so far no one seems to be asking the same question. Let me see if I can explain this adequately:

Say I'm a shoe store. On my web site I have three pages: one for men's shoes, one for women's shoes and one for kid's shoes. Let's say my shoe table has the field prod_group. So for the men's page the query would be q=prod_group:men. Now let's say one of the facets I want to provide is brand. q=prod_group:men&filter.field=brand

The issue I'm having is the facet results are showing all brands in my database, not just brands relevant to men's shoes. In other words, I already know that there are zero men's shoes in the "My Little Pony" line. I don't want to display My Little Pony in the brand facet. Setting facet.mincount isn't what I need either. If I run a query q=prod_group:men&fq=color:red&filter.field=brand, the client wants to show the consumer that the brand "Mighty Joe" doesn't have any red shoes so I still need facets with a facet count of 0.

Basically what I'm hoping for is facet results based on the results from the base query. So far the only way I see to accomplish this is to run two queries: the first with just the base query and setting facet.mincount = 1 to get a list of relevant facets and then the actual query with facet.mincount back to 0 so I can figure out when to grey out certain options that are no longer available based on facet choices already made.

Running two queries seems somewhat less than elegant. facet.pivot might help in the long run, but we're still using 3.x and I don't see that changing any time soon. Have I missed some query/schema/configuration option that will do what I need with one query?

asked May 16, 2015 in SOLR by rajesh
0 votes

3 Answers

0 votes
You can check for one option called Facet Excluding Filters, which will allow you to return the facets irrespective of the filter applied.

you filter on men fq=prod_group:men with facet.mincount 1 which will return all the brand facets applicable to this filter.
Then when you sub filter on some color fq=color:red you can get two facets back one with the results and one without the filter using the excluding filters
e.g. fq={!tag=dt}color:red&facet.field={!ex=dt key=nonfilter_brand}brand&facet.field=brand

This will give you both the facets with and without filter using which you can use to grey out the differences the client side. A bit of overhead on the client side, but you just need to perform a single query.
answered May 16, 2015 by rajesh
0 votes
Have you looked at the facet.query option. From the description it sounds like it will accomplish what you need.

This param allows you to specify an arbitrary query in the Lucene default syntax to generate a facet count. By default, faceting returns a count of the unique terms for a "field", while facet.query allows you to determine counts for arbitrary terms or expressions.
answered May 16, 2015 by rajesh
0 votes
makes the filtering applicable to the facet results too, which is not the case with q parameters.
answered May 16, 2015 by rajesh