简体   繁体   English

在弹性搜索中添加条件以过滤聚合

[英]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 无法解析名称为[count]的BaseAggregationBuilder:找不到解析器

"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: 这是因为没有称为count的度量聚合,而是使用value_count代替:

"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 . 经过讨论, terms聚合”比“ value_count更合适。 After fixing the mapping (which was text instead of keyword ), the query worked out correctly 修复了映射关系后(该texttext而不是keyword ),该查询可以正确计算出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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