【问题标题】:Find the number of documents with filter at a particular time in Elasticsearch在 Elasticsearch 中查找特定时间带有过滤器的文档数
【发布时间】:2020-09-27 02:21:37
【问题描述】:

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

{
  "id": "T12890ADSA12",
  "status": "ENDED",
  "type": "SAMPLE",
  "updatedAt": "2020-05-29T18:18:08.483Z",
  "audit": [
    {
      "event": "STARTED",
      "version": 1,
      "timestamp": "2020-04-30T13:41:25.862Z"
    },
    {
      "event": "INPROGRESS",
      "version": 2,
      "timestamp": "2020-05-14T17:03:09.137Z"
    },
    {
      "event": "INPROGRESS",
      "version": 3,
      "timestamp": "2020-05-17T17:03:09.137Z"
    },
    {
      "event": "ENDED",
      "version": 4,
      "timestamp": "2020-05-29T18:18:08.483Z"
    }
  ],
  "createdAt": "2020-04-30T13:41:25.862Z"
}

如果我想知道在给定的特定时间STARTED state 中的文档数量。我怎样才能做到这一点?它应该使用事件字段中每个事件的时间戳。

编辑:索引的映射如下:

{
  "id": "text",
  "status": "text",
  "type": "text",
  "updatedAt": "date",
  "events": [
    {
      "event": "text",
      "version": long,
      "timestamp": "date"
    }
  ],
  "createdAt": "date"
}

【问题讨论】:

  • 你能分享你的索引映射吗?
  • @Val,我已经添加了有问题的映射。

标签: elasticsearch count elasticsearch-aggregation elasticsearch-dsl elasticsearch-query


【解决方案1】:

为了实现您想要的,您需要确保events 数组是nested 类型,因为您需要对每个数组元素应用两个条件,这只有在@987654323 时才有可能@ 是嵌套的:

    "events" : {
      "type": "nested",                     <--- you need to add this
      "properties" : {
        "event" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "timestamp" : {
          "type" : "date"
        },
        "version" : {
          "type" : "long"
        }
      }
    },

然后您将能够运行以下nested 查询:

{
  "query": {
    "nested": {
      "path": "events",
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "events.date": {
                  "gte": "2020-06-08",
                  "lte": "2020-06-08"
                }
              }
            },
            {
              "term": {
                "events.event": "STARTED"
              }
            }
          ]
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多