【问题标题】:Understanding Aggregation results on ElasticSearch了解 ElasticSearch 上的聚合结果
【发布时间】:2018-11-16 15:48:55
【问题描述】:

我正在处理 ElasticSearch 中的特定查询。查询的目标是返回所有具有最新时间戳的唯一结果。 所以只是为了给出背景,在弹性搜索数据库中,每个具有不同时间戳的唯一字段“x”可以有多个条目。我希望 ES 查询返回每个唯一字段 x 的最新时间戳。 所以数据看起来像 ES 数据库中的:

{"x" : "1", "time": 1536574915}
{"x" : "2", "time": 1536574919}
{"x" : "1", "time": 1536574815}
{"x" : "2", "time": 1536574819}
{"x" : "3", "time": 1536574915}
{"x" : "4", "time": 1536574915}

预期输出是

{"x" : "1", "time": 1536574915}
{"x" : "2", "time": 1536574919}
{"x" : "3", "time": 1536574915}
{"x" : "4", "time": 1536574915}

我目前使用的查询是:

{
            "size": 0,
            "query": {
                "bool": {
                    "must": [],
                    "filter": {
                        "range": {
                            "time": {
                                "lte": "2019-11-16", Can give epoch conversion here
                                "format": "date_optional_time"
                                }
                            }
                        }
                    }
                },
            "aggs": {
                "group_by": {
                 "terms": {
                        "field": "x"
                        },
                    "aggs": {
                        "resource": {
                            "terms": {
                                "field": "time",
                                "size": 1,
                                "order": {
                                    "_key": "desc"
                                }

                            },

                            "aggs": {
                                "include_source": {
                                    "top_hits": {
                                        "from": 0,
                                        "size": 1,
                                        "_source": {}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

上述查询返回的结果有

[
    {
        "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoAgAAAAAAAAECFmtnNUY4dHFKUXVldXdQMkNSaE1femcAAAAAAAABAxZrZzVGOHRxSlF1ZXV3UDJDUmhNX3pn",
        "took": 227,
        "timed_out": false,
        "_shards": {
            "total": 2,
            "successful": 2,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 343533,
            "max_score": 0.0,
            "hits": [
                {
                }
            ]
},
   "aggregations": {
                "group_by": {
                    "doc_count_error_upper_bound": 4,
                    "sum_other_doc_count": 343513,
                    "buckets": [
                  { # here is the actual data.
                  }
           ]
        }
      }
    },
    {
       #another scroll_id. Removed the data as its huge.
   }
]

我的问题是,上述案例中的独特结果在哪里? 是在 [hits][hits] 之内还是在“聚合”之内?如果在聚合中,对于一百万条记录,聚合只返回 10 个结果。如果我依赖每个滚动列表中的 [hits][hits],那么结果是重复的。我试图了解,根据上述查询约束,我可以得到正确的唯一条目。还是查询格式不正确或缺少某些参数。 感谢任何帮助。 谢谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您的聚合不正确,因为您要检索每个 x 和 time 的热门匹配,而您的目标是检索每个 x 的最新匹配。您需要修改您的查询,如下所示,即您仅按x 聚合,而在您的top_hits 子聚合中,您通过减少time 对文档进行排序,并且只取最后一个。

    {
      "size": 0,
      "aggs": {
        "group_by": {
          "terms": {
            "field": "x"
          },
          "aggs": {
            "resource": {
              "top_hits": {
                "from": 0,
                "size": 1,
                "sort": {
                  "time": "desc"
                },
                "_source": {}
              }
            }
          }
        }
      }
    }
    

    您要查找的文档位于每个存储桶的 resource.hits.hits 部分:

      "aggregations" : {
        "group_by" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "1",
              "doc_count" : 2,
              "resource" : {
                "hits" : {
                  "total" : 2,
                  "max_score" : null,
                  "hits" : [
                    {
                      "_index" : "times",
                      "_type" : "doc",
                      "_id" : "PZt7G2cBJos57mIu0oy-",
                      "_score" : null,
                      "_source" : {
                        "x" : "1",
                        "time" : 1536574915
                      },
                      "sort" : [
                        1536574915
                      ]
                    }
                  ]
                }
              }
            },
            {
              "key" : "2",
              "doc_count" : 2,
              "resource" : {
                "hits" : {
                  "total" : 2,
                  "max_score" : null,
                  "hits" : [
                    {
                      "_index" : "times",
                      "_type" : "doc",
                      "_id" : "Ppt7G2cBJos57mIu0oy-",
                      "_score" : null,
                      "_source" : {
                        "x" : "2",
                        "time" : 1536574919
                      },
                      "sort" : [
                        1536574919
                      ]
                    }
                  ]
                }
              }
            },
            {
              "key" : "3",
              "doc_count" : 1,
              "resource" : {
                "hits" : {
                  "total" : 1,
                  "max_score" : null,
                  "hits" : [
                    {
                      "_index" : "times",
                      "_type" : "doc",
                      "_id" : "QZt7G2cBJos57mIu0oy-",
                      "_score" : null,
                      "_source" : {
                        "x" : "3",
                        "time" : 1536574915
                      },
                      "sort" : [
                        1536574915
                      ]
                    }
                  ]
                }
              }
            },
            {
              "key" : "4",
              "doc_count" : 1,
              "resource" : {
                "hits" : {
                  "total" : 1,
                  "max_score" : null,
                  "hits" : [
                    {
                      "_index" : "times",
                      "_type" : "doc",
                      "_id" : "Qpt7G2cBJos57mIu0oy-",
                      "_score" : null,
                      "_source" : {
                        "x" : "4",
                        "time" : 1536574915
                      },
                      "sort" : [
                        1536574915
                      ]
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    

    【讨论】:

    • 感谢您的简短解释。但是,如果您期望一个大于 10 的唯一“x”,上述查询是否成立?聚合是否会将您的结果限制为仅显示 10 个条目?我的记录大小是 100 万,并且有所有唯一的“x”。我可以期望在聚合桶中看到这 100 万条记录吗?如果没有,那么我如何查看所有 100 万条唯一记录?请帮助我理解这一点。
    猜你喜欢
    • 2014-10-22
    • 2018-12-11
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2019-08-22
    • 2017-10-29
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多