SOLR faceted-search to return facet counts as with OR operator

defekt19754 picture defekt19754 · May 17, 2013 · Viewed 8k times · Source

I'll try first explain what I would like to achieve by using faceted-search in SOLR, before setting the question. Imagine I have a parameter for dog which is describing for what is dog suitable for and one dog can have multiple choices. Let's call it "housing". So dog is suitable for:

  1. indoors
  2. outdoors
  3. children
  4. old people
  5. guarding.

So basically when I use faceted search on this parameter I get the results with my current database like this (url query facet=true&facet.field=housing):

indoors (74), outdoors(63), children(71), old people(65), guarding(31).

Now when I check the indoors parameter (url query fq=housing:1&facet=true&facet.field=housing) I get the results like this:

indoors (74), outdoors(53), children(60), old people(53), guarding(15).

It is showing me that if check next parameter "outdoors", there are 53 dog which are indoors AND outdoors. I would like to get the result, which would tell "How many dogs will be added to the result if I check next option". Because there can be dogs which are suitable for outdoors only. I would like to get results like this after checking first "indoors" parameter:
indoors (74), outdoors(83), children(78), old people(79), guarding(75)
OR in other way
indoors (+0), outdoors(+9), children(+4), old people(+5), guarding(+1)

Is it something like this possible in SOLR faceted-search? Or am I using incorrect tool for achieving this. Because basically when I check "indoors" and "outdoors" I get the proper result count from the query (url query fq=housing:(1 OR 2)&facet=true&facet.field=housing). Just facet counts are not the ones I expected.

Answer

Paige Cook picture Paige Cook · May 17, 2013

I think you will need to use the facet.query parameter, which will allow you to specify a query that will only apply to the faceting results and not the main query.

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.

In your case, I believe you will still return the facet counts for unique terms for a "field", but with some additional filters applied.