【问题标题】:Elasticsearch Terms Aggregation - for dynamic keys of an objectElasticsearch 术语聚合 - 用于对象的动态键
【发布时间】:2019-10-10 01:59:52
【问题描述】:

文档结构

Doc_1 {
"title":"hello",
"myObject":{
 "key1":"value1",
 "key2":"value2"
 }
}
Doc_2 {
"title":"hello world",
"myObject":{
 "key2":"value4",
 "key3":"value3"
 }
}
Doc_3 {
"title":"hello world2",
"myObject":{
 "key1":"value1",
 "key3":"value3"
 }
}

信息myObject包含动态键值对。

目标:我的目标是编写聚合查询返回唯一的所有动态键值对的数量。

尝试及说明:我可以通过这种方式轻松获得已知键的结果。

       {
        "size":0,
        "query":{
               "match":{"title":"hello"}
                },
        "aggs":{
               "key1Agg":{
                    "terms":{"field":"myObject.key1.keyword"}
                },
               "key2Agg":{
                    "terms":{"field":"myObject.key2.keyword"}
                },
               "key3Agg":{
                    "terms":{"field":"myObject.key3.keyword"}
               }
              }
          }

这是上述硬编码嵌套键聚合的典型结果。

{
...
"aggregations": {
    "key1Agg": {
        ...        
        "buckets": [
            {
                "key": "value1",
                "doc_count": 2
            }

        ]
    },
    "key2Agg": {
        ...
        "buckets": [
            {
                "key": "value2",
                "doc_count": 1
            },
            {
                "key": "value4",
                "doc_count": 1
            }

        ]
    },
    "key3Agg": {
       ...
        "buckets": [
            {
                "key": "value3",
                "doc_count": 2
            }

        ]
    }
}

}

现在我只想返回所有动态键值对的计数,即不在聚合查询中放置任何核心键名。

我正在使用 ES 6.3,提前致谢!!

【问题讨论】:

    标签: elasticsearch aggregation elasticsearch-6


    【解决方案1】:

    根据您提供的信息,myObject 似乎属于object datatype,而不是nested datatype

    好吧,如果不修改您拥有的数据,就没有简单的方法可以做,您可以做什么,可能最简单的解决方案是包含一个额外的字段,比如说我们称之为myObject_list,它的类型是@ 987654327@ 文件如下:

    示例文件:

    POST test_index/_doc/1
    {
     "title":"hello",
      "myObject":{
       "key1":"value1",
       "key2":"value2"
      },
      "myObject_list": ["key1_value1", "key2_value2"]     <--- Note this
    }
    
    POST test_index/_doc/2
    {
     "title":"hello world",
      "myObject":{
       "key2":"value4",
       "key3":"value3"
      },
      "myObject_list": ["key2_value4", "key3_value3"]     <--- Note this
    }
    
    POST test_index/_doc/3
    {
     "title":"hello world2",
      "myObject":{
       "key1":"value1",
       "key3":"value3"
      },
      "myObject_list": ["key1_value1", "key3_value3"]     <--- Note this
    }
    

    您可以进行如下简单的查询:

    请求查询:

    POST test_index/_search
    {
      "size": 0,
      "aggs": {
        "key_value_aggregation": {
          "terms": {
            "field": "myObject_list",              <--- Make sure this is of keyword type
            "size": 10
          }
        }
      }
    }
    

    请注意,我在这里使用了Terms Aggregation

    回应:

    {
      "took" : 406,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "key_value_aggregation" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "key1_value1",
              "doc_count" : 2
            },
            {
              "key" : "key3_value3",
              "doc_count" : 2
            },
            {
              "key" : "key2_value2",
              "doc_count" : 1
            },
            {
              "key" : "key2_value4",
              "doc_count" : 1
            }
          ]
        }
      }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢@kamal,我已将标题问题更新为对象类型而不是嵌套类型。是不是意味着不添加相应的新字段类型关键字就没有其他方法可以实现这一点?
    • 好吧,我认为它可以通过无痛脚本完成,但我不确定(我知道这可以为 nested 类型完成,但不确定 object 类型)。需要评估和查看。让我看看这个周末我能不能花点时间在这上面,然后告诉你。希望没问题!!仅供参考,如果您想尝试参考这个答案,看看它是否有帮助。 stackoverflow.com/a/53192844/3838328
    • 另外更重要的是,不推荐使用无痛脚本,因为它可能会影响性能,在大型数据集上进行高计算或简单计算时更是如此。无论如何,最佳解决方案将是我发布的答案。
    猜你喜欢
    • 2014-07-09
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2021-06-06
    • 2021-06-06
    • 2015-11-06
    • 2015-01-21
    相关资源
    最近更新 更多