简体   繁体   中英

Add condition to filter aggregation in elastic search

I want the count of each values of a variable based on some filter applied in elastic search. For example, I want all the age groups but on the filter that the students are from California.

The age groups is text field and contains an array like this,

"age_group": ["5-6-years", "6-7-years"]

I kinda want a query like this but this ain't working. It throws an error saying

unable to parse BaseAggregationBuilder with name [count]: parser not found

"student_aggregation": {
    "nested": {
        path": "students"
    },
    "aggs": {
        "available": {
            "filter": {
                "term": { "students.place_of_birth": "California" }
            },
            "aggs" : {
                "age_group" : { "count" : { "field" : "students.age_group" } }
            }
        }
    }
}

Request help from you troops.

That's because there's no metric aggregation called count but value_count instead:

"student_aggregation": {
    "nested": {
        path": "students"
    },
    "aggs": {
        "available": {
            "filter": {
                "term": { "students.gender": "boys" }
            },
            "aggs" : {
                "age_group" : { "value_count" : { "field" : "students.age_group" } }
                                     ^^^
                                     |||
            }
        }
    }
}

UPDATE :

After discussions, the terms aggregation was more appropriate than value_count . After fixing the mapping (which was text instead of keyword ), the query worked out correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM