简体   繁体   English

Elasticsearch 对嵌套字段的复合聚合查询

[英]Elasticsearch composite aggregate query on nested fields

I've a question on an aggregation on nested objects.我有一个关于嵌套对象聚合的问题。 Document is like:文档是这样的:

{
    "features": [{
            "key": "key1",
            "values": ["A", "B"]
        },
        {
            "key": "key2",
            "values": ["C", "D"]
        },
        {
            "key": "key2",
            "values": ["E"]
        }
    ]
}

where 'features' is a nested object.其中 'features' 是一个嵌套对象。 I can aggregate and get distinct values from key and values, but I need to get a combined bucket aggregation, where I need:我可以聚合并从键和值中获取不同的值,但我需要获得组合的存储桶聚合,我需要:

key1 -> A,B key2 -> C,D,E key1 -> A,B key2 -> C,D,E

Is composite aggregation that has to be used?是否必须使用复合聚合? Or which is the proper aggregation to use?或者哪个是要使用的正确聚合? Java samples are also welcome!也欢迎 Java 样本!

Thanks!!!谢谢!!!

You don't really need composite for this.你真的不需要composite The following should be fine:以下应该没问题:

{
  "size": 0,
  "aggs": {
    "nested_aggs": {
      "nested": {
        "path": "features"
      },
      "aggs": {
        "by_key": {
          "terms": {
            "field": "features.key.keyword"
          },
          "aggs": {
            "by_values": {
              "terms": {
                "field": "features.values.keyword"
              }
            }
          }
        }
      }
    }
  }
}

assuming your mapping looks like this假设你的映射看起来像这样

{
  "mappings":{
    "properties":{
      "features":{
        "type":"nested",
        "properties":{
          "key":{
            "type":"text",
            "fields":{
              "keyword":{
                "type":"keyword",
                "ignore_above":256
              }
            }
          },
          "values":{
            "type":"text",
            "fields":{
              "keyword":{
                "type":"keyword",
                "ignore_above":256
              }
            }
          }
        }
      }
    }
  }
}

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

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