【问题标题】:Add condition to filter aggregation in elastic search在弹性搜索中添加条件以过滤聚合
【发布时间】:2019-08-29 20:11:38
【问题描述】:

我想根据弹性搜索中应用的某些过滤器计算变量的每个值。例如,我想要所有年龄组,但在过滤器上的学生来自加利福尼亚。

年龄组是文本字段,包含这样的数组,

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

我有点想要这样的查询,但这不起作用。它会抛出一个错误,说

无法解析名称为 [count] 的 BaseAggregationBuilder:找不到解析器

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

向你的部队请求帮助。

【问题讨论】:

  • 你能定义“不工作”吗?会发生什么,你会期待什么?
  • @Val 我添加了从查询中得到的错误。你能调查一下吗?
  • @Val 表示计数聚合不起作用,但 avg 和其他功能起作用。

标签: elasticsearch


【解决方案1】:

这是因为没有称为 count 的度量聚合,而是 value_count

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

更新

经过讨论,terms 聚合比 value_count 更合适。修复映射后(是 text 而不是 keyword),查询正常运行

【讨论】:

  • 我收到以下错误:“默认情况下,在文本字段上禁用字段数据。在 [students.age_group] 上设置 fielddata=true,以便通过反转倒排索引将字段数据加载到内存中。注意但是,这可能会占用大量内存。或者使用关键字字段。”。你能告诉我如何解决这个问题吗?
  • 尝试改用students.age_group.keyword
  • 我不明白我到底该如何使用students.age_group.keyword?
  • 在您的查询中将 students.age_group 替换为 students.age_group.keyword
  • 然后返回 0 作为计数,如下所示:{"available" : { "doc_count" : 21024, "variant_age_group" : { "value" : 0 } }。我已更新问题以显示年龄组数组的存储方式
猜你喜欢
  • 1970-01-01
  • 2020-06-20
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2014-11-10
相关资源
最近更新 更多