【问题标题】:How do I query a null date inside an array in elasticsearch?如何在elasticsearch中查询数组内的空日期?
【发布时间】:2019-04-18 22:02:43
【问题描述】:

在弹性搜索查询中,我试图搜索具有一系列批准通知的 Document 对象。当dateCompleted 填充了日期时,通知被认为是完整的,当dateCompleted 不存在或存在null 时,通知被认为是待处理的。如果文档不包含一系列批准通知,则它不在搜索范围内。

我知道将 null_value 用于字段 dateCompleted 并将其设置为任意旧日期,但这对我来说似乎很不自然。

我尝试使用带有 must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted 的 Bool 查询,但如果文档包含完整和待处理的approvalNotifications,这将不起作用。例如它只返回下面 ID 为 2 的文档。我期待找到 ID 为 1 和 2 的文档。

如何使用 elasticsearch 查找待批准通知?

PUT my_index/_mapping/Document

 "properties" : {
  "doc" : {
    "properties" : {
      "approvalNotifications" : {
        "properties" : {
          "approvalBatchId" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "approvalTransitionState" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "approvedByUser" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "dateCompleted" : {
            "type" : "date"
          }
        }
      }
    }
  }
}

文件:

{
    "id": 1,
    "status": "Pending Notifications",
    "approvalNotifications": [
        {
            "approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
            "dateCompleted": "2018-11-15T16:09:15.346+0000"
        },
        {
            "approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
        }
    ]
}

{
    "id": 2,
    "status": "Pending Notifications",
    "approvalNotifications": [
        {
            "approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
        }
    ]
}

{
    "id": 3,
    "status": "Complete",
    "approvalNotifications": [
        {
            "approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
            "dateCompleted": "2018-11-15T16:09:15.346+0000"
        },
        {
            "approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
            "dateCompleted": "2018-11-16T16:09:15.346+0000"            
        }
    ]
}

{
    "id": 4
    "status": "No Notifications"
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您快到了,您可以通过为 "approvalNotifications" 字段使用 nested 数据类型来实现所需的行为。

    Elasticsearch 会展平您的 approvalNotifications 对象,将它们的子字段视为原始文档的子字段。 nested 字段将告诉 ES 将每个内部对象索引为隐式独立对象,尽管与原始对象相关。

    要查询nested 对象,应使用nested query

    希望有帮助!

    【讨论】:

    • 我可以试试。看来我需要重新创建整个索引,因为不允许将类型更改为嵌套在 /my_index/_mapping API 中。
    • @dukthrash 是的,不幸的是重新索引是必要的。
    猜你喜欢
    • 2015-09-19
    • 2020-09-28
    • 2021-04-16
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2021-09-29
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多