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

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.

asked May 16, 2015 in SOLR by rajesh
0 votes

2 Answers

0 votes
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.
answered May 16, 2015 by rajesh
0 votes
I think next way should work:

fq={!tag=housing}housing:1&facet.field={!key=housing}housing&facet.field={!key=housing_ex ex=housing}housing
And you should get something like:

"facet_counts": {
  "facet_queries": {},
  "facet_fields": {
    "housing": [
      "1", 74,
      "2", 53,
      "3", 60,
      "4", 53,
      "5", 15
    "housing_ex": [
      "1", 74,
      "2", 63,
      "3", 71,
      "4", 65,
      "5", 31
Then you should do: housing_ex - housing. And the result is:

1: +0
2: +10
3: +11
4: +12
5: +16
numFound + housing_ex - housing:

1: 74
2: 84
3: 85
4: 86
5: 90
facet.query is not suitable for a lot of facet values.
answered May 16, 2015 by rajesh