Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· DevOps Zone ·
Free Resource

Easily enforce open source policies in real time and reduce MTTRs from six weeks to six seconds with the Sonatype Nexus Platform. See for yourself - Free Vulnerability Scanner. 

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>

Automate open source governance at scale across the entire software supply chain with the Nexus Platform. Learn more.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}