gpt4 book ai didi

elasticsearch - 每天的文档数存储桶并应用了一些过滤器

转载 作者:行者123 更新时间:2023-12-03 01:15:27 25 4
gpt4 key购买 nike

我在elasticsearch中有一些文档,其中每个文档如下所示:

{
"id": "T12890ADSA12",
"status": “CREATED”,
"type": “ABC”,
"updatedAt": "2020-05-29T18:18:08.483Z",
"createdAt": "2020-04-30T13:41:25.862Z"
}
对于此文档结构,我想获取所有状态为CREATED或SCHEDULED且TYPE为ABC的文档。在这些过滤的文档中,我想基于currentDate-daysbucket中的createdAt汇总文档的数量。例如。
  • 创建日期为今天的日期->今天创建的文档数
  • 创建日期为昨天的日期->昨天创建的文档数

  • 最后7天也是如此。
    有一个简单的方法可以在单个查询中执行此操作吗?

    最佳答案

    请找到以下映射,示例文档,汇总查询和响应:
    对应:

    PUT my_date_index
    {
    "mappings": {
    "properties": {
    "id": {
    "type": "keyword"
    },
    "status": {
    "type": "keyword"
    },
    "type": {
    "type": "keyword"
    },
    "updatedAt": {
    "type": "date"
    },
    "createdAt": {
    "type": "date"
    }
    }
    }
    }
    样本文件:
    POST my_date_index/_doc/1
    {
    "id": "T12890ADSA12",
    "status": "CREATED",
    "type": "ABC",
    "updatedAt": "2020-05-29T18:18:08.483Z",
    "createdAt": "2020-07-06T05:00:00.000Z"
    }

    POST my_date_index/_doc/2
    {
    "id": "T12890ADSA13",
    "status": "SCHEDULED",
    "type": "ABC",
    "updatedAt": "2020-05-29T18:18:08.483Z",
    "createdAt": "2020-07-05T13:41:25.862Z"
    }

    POST my_date_index/_doc/3
    {
    "id": "T12890ADSA14",
    "status": "SCHEDULED",
    "type": "ABC",
    "updatedAt": "2020-05-29T18:18:08.483Z",
    "createdAt": "2020-07-04T06:00:00.000Z"
    }

    POST my_date_index/_doc/4
    {
    "id": "T12890ADSA15",
    "status": "SCHEDULED",
    "type": "ABC",
    "updatedAt": "2020-05-29T18:18:08.483Z",
    "createdAt": "2020-07-03T07:00:00.000Z"
    }
    查询请求:
    POST my_date_index/_search
    {
    "size": 0, <----- Remove this to return documents too
    "query": {
    "bool": {
    "must": [
    {
    "term": {
    "type": "ABC"
    }
    },
    {
    "range": {
    "createdAt": {
    "gte": "now-7d",
    "lte": "now"
    }
    }
    }
    ],
    "should": [
    {
    "term": {
    "status": "SCHEDULED"
    }
    },
    {
    "term": {
    "status": "CREATED"
    }
    }
    ],
    "minimum_should_match": 1
    }
    },
    "aggs": {
    "my_date": {
    "date_histogram": {
    "field": "createdAt",
    "calendar_interval": "day",
    "order": {
    "_key": "desc"
    }
    }
    }
    }
    }
    请注意,我首先根据日期和您提供的条件过滤了文档。
    这将返回所有文档。我已应用 date histogram查询来获取该日期范围内每一天的文档。
    响应:
    {
    "took" : 0,
    "timed_out" : false,
    "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
    },
    "hits" : {
    "total" : {
    "value" : 4,
    "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
    },
    "aggregations" : {
    "my_date" : {
    "buckets" : [
    {
    "key_as_string" : "2020-07-06T00:00:00.000Z",
    "key" : 1593993600000,
    "doc_count" : 1
    },
    {
    "key_as_string" : "2020-07-05T00:00:00.000Z",
    "key" : 1593907200000,
    "doc_count" : 1
    },
    {
    "key_as_string" : "2020-07-04T00:00:00.000Z",
    "key" : 1593820800000,
    "doc_count" : 1
    },
    {
    "key_as_string" : "2020-07-03T00:00:00.000Z",
    "key" : 1593734400000,
    "doc_count" : 1
    }
    ]
    }
    }
    }
    希望这可以帮助!

    关于elasticsearch - 每天的文档数存储桶并应用了一些过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62749322/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com