Big Data/Analytics Zone is brought to you in partnership with:

Coming from a background of Aerospace Engineering, John soon discovered that his true interest lay at the intersections of information technology and entrepreneurship (and when applicable - math). In early 2011, John stepped away from his day job to take up software consulting. Finally John found permanent employment at Opensource Connections where he currently consults large enterprises about full-text search and Big Data applications. Highlights to this point have included prototyping the future of search with the US Patent and Trademark Office, implementing the search syntax used by patent examiners, and building a Solr search relevancy tuning framework called SolrPanl. John is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

Facet Over The Same Field Multiple Times With Different Filters in Solr

02.21.2013
| 2556 views |
  • submit to reddit

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?

Let’s 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.

Here’s 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:

<lst name="facet_fields">
        </lst><lst name="RecentAnswerCount">
            <int name="2">2543</int>
            <int name="1">2059</int>
            <int name="3">1831</int>
            <int name="4">1015</int>
            <int name="5">514</int>
            <int name="6">222</int>
            <int name="7">114</int>
            <int name="0">95</int>
            <int name="8">77</int>
            <int name="9">44</int>
            <int name="10">24</int>
            <int name="11">12</int>
            <int name="12">9</int>
            <int name="14">8</int>
            <int name="13">6</int>
            <int name="15">4</int>
            <int name="16">4</int>
            <int name="18">3</int>
            <int name="17">2</int>
            <int name="19">2</int>
            <int name="21">2</int>
            <int name="22">2</int>
            <int name="20">1</int>
            <int name="23">1</int>
            <int name="25">1</int>
            <int name="26">1</int>
            <int name="28">1</int>
            <int name="24">0</int>
            <int name="42">0</int>
            <int name="116">0</int>
        </lst>
        <lst name="TotalAnswerCount">
            <int name="2">4862</int>
            <int name="3">3729</int>
            <int name="1">3582</int>
            <int name="4">2116</int>
            <int name="5">1172</int>
            <int name="6">560</int>
            <int name="7">297</int>
            <int name="8">183</int>
            <int name="0">173</int>
            <int name="9">109</int>
            <int name="10">62</int>
            <int name="11">38</int>
            <int name="12">26</int>
            <int name="14">19</int>
            <int name="13">16</int>
            <int name="15">12</int>
            <int name="16">7</int>
            <int name="18">6</int>
            <int name="19">5</int>
            <int name="21">5</int>
            <int name="17">3</int>
            <int name="22">3</int>
            <int name="20">2</int>
            <int name="23">1</int>
            <int name="24">1</int>
            <int name="25">1</int>
            <int name="26">1</int>
            <int name="28">1</int>
            <int name="42">1</int>
            <int name="116">1</int>
        </lst>
Published at DZone with permission of John Berryman, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)