Blog

# Convenient Solr Feature: Facet over the same field multiple times with different filters

Did you know that you can facet over the same field more than once? Did you know that you can rename the facet field? Did you know that you have display the facet with different filters?

Lets say I want to look at the number of StackOverflow answers for recent questions as compared to the number of answers across all questions ever asked. I did this be creating a filter query tagged with “moreRecent.” And then I faceted over the AnswerCount field twice, once with the alias of RecentAnswerCount and once with the alias of TotalAnswerCount. The later facet excluded the “moreRecent” filter query.

Heres the query:

http://localhost:8983/solr/collection1/select?q=*:*&facet=on&fq={!tag=moreRecent}CreationDate:[2011-08-05T19:34:49.473Z TO NOW]&facet.field={!key=RecentAnswerCount}AnswerCount&facet.field={!key=TotalAnswerCount ex=moreRecent}AnswerCount

And here is the response:

                        2543            2059            1831            1015            514            222            114            95            77            44            24            12            9            8            6            4            4            3            2            2            2            2            1            1            1            1            1            0            0            0                            4862            3729            3582            2116            1172            560            297            183            173            109            62            38            26            19            16            12            7            6            5            5            3            3            2            1            1            1            1            1            1            1