skip to Main Content

I have a use case where i would need to filter in the aggregated value from the facet.

Does facet filter allow that?

Example:
Getting the sum of a number field that are greater than 0 grouping by country

So my Jason facet would look like

json.facet={cty:{type:terms,field:country,facet:{sum_value:”sum(population)”}}}

In the above request I need to add a filter to return only those cty buckets where sum_value is >100

2

Answers


  1. Well, we have faced the similar task before, and I would say that there is no way Solr can filter calculated values. Either you can fetch all the facet by setting limit equal to -1 and then there will be some wrapper(WEB API) you need to write over your search application which will filter the facet list.

    json.facet={cty:{type:terms,field:country,limit:-1,facet:{sum_value:”sum(population)”}}}
    

    Or you can Pre-calculate the sum of population for each country before indexing. Look at the below table.

    |Country|Population| Total_Population|

    | C1 | 12 | 25 |
    | C1 | 13 | 25 |
    | C2 | 14 | 49 |
    | C2 | 35 | 49 |

    Later on, you can directly filter on Total_Population field by using filter query. Also, you will be saving lot of time by not doing aggregations in real time.

    Login or Signup to reply.
  2. Solr supports sorting facets by nested functions. You can sort based on your facet and limit the response. For your use case:

    json.facet={
    cty:{
        type:terms,
        field:country,
        sort: "sum_value desc"
        facet:
            {
                sum_value:”sum(population)”
            }
        }
    }
    

    Also you can pass limit=100 to filter out top 100 sum_value

    Source

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search