Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict aggregation to results of filter

Tags:

Given a set of documents that have a given value in one field, I want to know how many documents there are that have each value for a second field.

I've tried to do that with a terms aggregation with the following query:

{
    "size": 0,
    "filter": {
        "term": {
            "field1": "value"
        }
    },
    "aggregations": {
        "field2" : {
            "terms" : { "field" : "field2" }
        }
    }
}

but the returned counts show the number of documents with each value for the second field in the whole index, and not restricted to those documents with a given value for the first field.

What am I doing wrong?

like image 578
user2463201 Avatar asked Apr 02 '14 16:04

user2463201


People also ask

What is an aggregate filter?

With Aggregated Filters, you are now able to filter on an aggregated column value. Aggregated Filters can be applied to string, amount, or date columns and are available to be used everywhere Filters are applied: Inside the Analyzer. As a Quick Filter.

What is aggregation in Elasticsearch?

In Elasticsearch, an aggregation is a collection or the gathering of related things together. The aggregation framework collects data based on the documents that match a search request which helps in building summaries of the data.

What aggregation means?

Terms aggregationedit. A multi-bucket value source based aggregation where buckets are dynamically built - one per unique value. The field can be Keyword, Numeric, ip , boolean , or binary .


1 Answers

Have you tried using filtered query?

{
    "query": {
        "filtered": {
           "query": {
                "match_all": {}
           },
           "filter": {
               "term": {
                  "field1": "value"
               }
           }
        }
    },
    "aggregations": {
        "field2": {
           "terms": { "field": "field2" }
        }
    }
}
like image 145
Mihai Ionescu Avatar answered Oct 21 '22 02:10

Mihai Ionescu