15

What I want to achieve: I want my "age" aggregation to not be filtered by the query filter and I want to be able to apply filters to it.

So if I start with this query:

{
    "query":{
        "filtered":{
            "filter":{ "terms":{ "family_name":"Brown" } } //filter_1
        }
    },
    "aggs":{
        "young_age":{
            "filter":{
                "range":{ "lt":40, "gt":18 } //filter_2
            },
            "aggs":{
                "age":{
                    "terms":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

My aggregation "young_age" will be filtered by both filter_1 and filter_2. I don't want my aggregation to be filtered by filter_1.

As I was looking into the documentation, I thought global aggregation would solve my problem, and I wrote that query:

{
    "query":{
        "filtered":{
            "filter":{ "terms":{ "family_name":"Brown" } } //filter_1
        }
    },
    "aggs":{
        "young_age":{
            "global":{}, //<----------- add global
            "filter":{
                "range":{ "lt":40, "gt":18 } //filter_2
            },
            "aggs":{
                "age":{
                    "terms":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

But then elastic search complains about my filter_2:

""" Found two aggregation type definitions [age] in [global] and [filter] """

And of course if I remove the filter_2:

{
    "query":{
        "filtered":{
            "filter":{
                "terms":{
                    "family_name":"Brown"
                }
            }
        }
    },
    "aggs":{
        "young_age":{
            "global":{},
            "aggs":{
                "age":{
                    "terms":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

Then my aggregation won't be filtered by filter_1 (as expected).

So how am I suppose to apply filter_2 to my global aggregation? Or how am I supposed to achieved that? I remember writing something similar with the facet filters...

2 Answers 2

10

In my opinion this is the typical use case of a post_filter. As the doc says:

The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated

Your query will look like:

{
    "post_filter":{
       "terms":{
            "family_name":"Brown" //filter_1
        }
    },
   "aggs":{
        "young_age":{
            "filter":{
                "range":{ "lt":40, "gt":18 } //filter_2
            },
            "aggs":{
                "age":{
                    "terms":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

In this case the search hits are all the documents in the index. Then the aggregation is calculated (before filter_1). And after that the post_filter with the filter_1 will be executed.

Edit: As you said in your commend you have many aggregations and only one that shouldn't be affected by filter_1 I fixed your query using global aggregation

{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "family_name": "Brown"
        }
      }
    }
  },
  "aggs": {
    "young_age": {
      "global": {},
      "aggs": {
        "filter2": {
          "filter": {
            "range": {
              "lt": 40,
              "gt": 18
            }
          },
          "aggs": {
            "age": {
              "terms": {
                "field": "age"
              }
            }
          }
        }
      }
    }
  }
}
4
  • 2
    That would work but actually I've got many aggregations and only one of them shouldn't be filtered by filter_1. So with your solution that means that I would have to replicate the post_filter for those aggregations which I find a bit annoying (and also maybe not that good from the performance point of view?) Commented Jul 29, 2015 at 1:50
  • 1
    @adrienbourgeois I fixed the query using global aggregation, let me know if it works for you :)
    – moliware
    Commented Jul 29, 2015 at 8:42
  • Thanks mate it's working like a charm! I've accepted your answer. cheers Commented Aug 27, 2015 at 6:31
  • Thank you for the second (edited) answer. I was losing my head until I found your filtered sub-aggregation idea.
    – maricn
    Commented Nov 16, 2017 at 10:26
1

globals and filters are not allowed at same level. you have to put filter at one level inside to global aggregation.

something like this should do for you.

{
    "query":{
        "filtered":{
            "filter":{
                "terms":{
                    "family_name":"Brown"
                }
            }
        }
    },
    "aggs":{
        "young_age":{
            "global":{},
            "aggs":{
                "filter": {"term": {"family_name": "Brown"}}, #or {"bool": {"filter": {"term": {"family_name": "Brown"}}}}
                "aggs": {
                    "age":{
                        "terms":{
                            "field":"age"
                        }
                    }
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.