【问题标题】:ElasticSearch terms aggregation but need other fields in the outputElasticSearch 术语聚合但需要输出中的其他字段
【发布时间】:2019-05-22 19:45:32
【问题描述】:

我的 Elastic 搜索文档如下所示。

{
 doc_id: 1,
 type: "type-1",
 user: "user1",
 color: "red",
 timestamp: epoch time here
},
{
 doc_id: 2,
 type: "type-2",
 user: "user2",
 color: "blue",
 timestamp: epoch time here
},
{
 doc_id: 3,
 type: "type-3",
 user: "user3",
 color: "red",
 timestamp: epoch time here
},

每种颜色可以有多个文档。我的要求是根据时间戳获取每种颜色的最新文档,因此每种颜色(红色、蓝色等)最多有一个文档。最后,我想过滤掉那些时间戳超过 2 天的带有颜色的文档。所以这最后一步进一步过滤掉了颜色。我请求帮助使用 Elastic Search 执行此操作。

【问题讨论】:

    标签: database elasticsearch nosql elasticsearch-aggregation


    【解决方案1】:

    映射:

    {
      "textindex" : {
        "mappings" : {
          "properties" : {
            "color" : {
              "type" : "keyword",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "doc_id" : {
              "type" : "long"
            },
            "timestamp" : {
              "type" : "date"
            },
            "type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "user" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
    

    数据:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "textindex",
            "_type" : "_doc",
            "_id" : "kN_D4moBLcHQX6h0M1GZ",
            "_score" : 1.0,
            "_source" : {
              "doc_id" : 1,
              "type" : "type-1",
              "user" : "user1",
              "color" : "red",
              "timestamp" : "2019-05-23"
            }
          },
          {
            "_index" : "textindex",
            "_type" : "_doc",
            "_id" : "kd_D4moBLcHQX6h0PFHp",
            "_score" : 1.0,
            "_source" : {
              "doc_id" : 2,
              "type" : "type-2",
              "user" : "user2",
              "color" : "blue",
              "timestamp" : "2019-05-23"
            }
          },
          {
            "_index" : "textindex",
            "_type" : "_doc",
            "_id" : "kt_D4moBLcHQX6h0QlHi",
            "_score" : 1.0,
            "_source" : {
              "doc_id" : 3,
              "type" : "type-3",
              "user" : "user3",
              "color" : "red",
              "timestamp" : "2019-05-22"
            }
          },
          {
            "_index" : "textindex",
            "_type" : "_doc",
            "_id" : "k9_I4moBLcHQX6h0M1GF",
            "_score" : 1.0,
            "_source" : {
              "doc_id" : 4,
              "type" : "type-4",
              "user" : "user4",
              "color" : "yello",
              "timestamp" : "2019-05-20"
            }
          }
        ]
      }
    }
    

    查询:- 我正在使用“top_hits”来获取按时间戳降序排序的顶级记录

    GET textindex/_search
    {
      "size": 0,
      "aggs": {
        "top_color": {
          "terms": {
            "field": "color"
          },
          "aggs": {
            "discard_old_dates": {
              "date_range": {
                "field": "timestamp",
                "ranges": [
                  {
                    "from": "now-2d/d",
                    "to": "now"
                  }
                ]
              },
              "aggs": {
                "top_team_hits": {
                  "top_hits": {
                    "sort": [
                      {
                        "timestamp": {
                          "order": "desc"
                        }
                      }
                    ],
                    "_source": {
                      "include": [
                        "doc_id",
                        "type",
                        "user",
                        "color",
                        "timestamp"
                      ]
                    },
                    "from": 0,
                    "size": 1
                  }
                }
              }
            }
          }
        }
      }
    }
    

    结果:

    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "top_color" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : "red",
              "doc_count" : 2,
              "discard_old_dates" : {
                "buckets" : [
                  {
                    "key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
                    "from" : 1.5583968E12,
                    "from_as_string" : "2019-05-21T00:00:00.000Z",
                    "to" : 1.558583729019E12,
                    "to_as_string" : "2019-05-23T03:55:29.019Z",
                    "doc_count" : 2,
                    "top_team_hits" : {
                      "hits" : {
                        "total" : {
                          "value" : 2,
                          "relation" : "eq"
                        },
                        "max_score" : null,
                        "hits" : [
                          {
                            "_index" : "textindex",
                            "_type" : "_doc",
                            "_id" : "kN_D4moBLcHQX6h0M1GZ",
                            "_score" : null,
                            "_source" : {
                              "color" : "red",
                              "type" : "type-1",
                              "doc_id" : 1,
                              "user" : "user1",
                              "timestamp" : "2019-05-23"
                            },
                            "sort" : [
                              1558569600000
                            ]
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            },
            {
              "key" : "blue",
              "doc_count" : 1,
              "discard_old_dates" : {
                "buckets" : [
                  {
                    "key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
                    "from" : 1.5583968E12,
                    "from_as_string" : "2019-05-21T00:00:00.000Z",
                    "to" : 1.558583729019E12,
                    "to_as_string" : "2019-05-23T03:55:29.019Z",
                    "doc_count" : 1,
                    "top_team_hits" : {
                      "hits" : {
                        "total" : {
                          "value" : 1,
                          "relation" : "eq"
                        },
                        "max_score" : null,
                        "hits" : [
                          {
                            "_index" : "textindex",
                            "_type" : "_doc",
                            "_id" : "kd_D4moBLcHQX6h0PFHp",
                            "_score" : null,
                            "_source" : {
                              "color" : "blue",
                              "type" : "type-2",
                              "doc_id" : 2,
                              "user" : "user2",
                              "timestamp" : "2019-05-23"
                            },
                            "sort" : [
                              1558569600000
                            ]
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            },
            {
              "key" : "yello",
              "doc_count" : 1,
              "discard_old_dates" : {
                "buckets" : [
                  {
                    "key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
                    "from" : 1.5583968E12,
                    "from_as_string" : "2019-05-21T00:00:00.000Z",
                    "to" : 1.558583729019E12,
                    "to_as_string" : "2019-05-23T03:55:29.019Z",
                    "doc_count" : 0,
                    "top_team_hits" : {
                      "hits" : {
                        "total" : {
                          "value" : 0,
                          "relation" : "eq"
                        },
                        "max_score" : null,
                        "hits" : [ ]
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    

    【讨论】:

    • 答案缺少关键部分,即用于将结果添加到答案中的查询/聚合。答案也缺少任何解释。
    • 谢谢你们。
    猜你喜欢
    • 2016-01-22
    • 1970-01-01
    • 2018-10-18
    • 2014-05-12
    • 2015-01-21
    • 2015-03-05
    • 1970-01-01
    • 2014-07-09
    • 2017-10-27
    相关资源
    最近更新 更多