8

I want to filter out documents whose field 'A' is equal to 'a', and I want to facet the field 'A' at the same time, excluding of course the previous filter. I know that you can put the filter 'outside' the query in order to get the facets without that filter applied, like:

ElasticSearch

{
   "query : { "match_all" : { } },  
   "filter" : { "term : { "A" : "a" } },
   "facets" : { 
      "A" : { "terms" : { "field" : "A" } }  //this should exclude the filter A:a
   }
}

SOLR

&q=:*:*
&fq={!tag=Aa}A:a
&facet=true&facet.field={!ex=Aa}A

This is very nice, but what happens if i have multiple filters and facets that each one should exclude each other? Example:

filter=A:a
filter=B:b
filter=C:c

facet={exclude filter A:a}A
facet={exclude filter B:b}B
facet={exclude filter C:c}C

That is, for facet A I want to keep all filters except A:a, for facet B all except B:b, and so on. The most obvious way would be to do n queries (one per each of the n facets), but I'd like to stay away from that.

Alexey Kosov
  • 3,010
  • 2
  • 23
  • 32
  • Ever managed to get this working? – Mauricio Scheffer Nov 06 '14 at 15:51
  • 2
    Yes, I had to move all filters to "post filter" ( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-post-filter.html ) and build a filter for each aggregation excluding unnecessary filter ( http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html ). – Alexey Kosov Nov 07 '14 at 10:23
  • I seem to have worked around it by aggregating scoped to `global` and then selecting the appropriate filters for each aggregation. – Mauricio Scheffer Nov 07 '14 at 11:06
  • Related question: http://stackoverflow.com/q/12014571/490018 – Sergey Vyacheslavovich Brunov Nov 25 '15 at 15:43

1 Answers1

2

The global scope provides access to every document, you can then add the same filters you used for the main query.

I gave an example with global scope in this related topic

Could you give any feedback about performance issue with post_filter ?

Community
  • 1
  • 1