ElasticSearch术语聚合,但在输出中需要其他字段

时间:2019-05-22 19:45:33

标签: database elasticsearch nosql elasticsearch-aggregation

我的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时,我需要帮助。

1 个答案:

答案 0 :(得分: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" : [ ]
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }