elasticsearch - 如何在 elasticsearch 中查询数组内的空日期?

标签 elasticsearch

在 elasticsearch 查询中,我试图搜索具有批准通知数组的文档对象。当 dateCompleted 填入日期时,通知被视为已完成,而当 dateCompleted 不存在或存在且为 null 时,通知被视为待处理。如果文档不包含一系列批准通知,则它不在搜索范围内。

我知道将 null_value 用于字段 dateCompleted 并将其设置为某个任意的旧日期,但这对我来说似乎很老套。

我尝试使用 Bool 查询 must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted 但如果文档包含完整和待处理的 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"
}

最佳答案

你就快完成了,你可以通过使用 nested 实现所需的行为“approvalNotifications” 字段的数据类型。

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

查询嵌套对象应该使用nested query .

希望对您有所帮助!

关于elasticsearch - 如何在 elasticsearch 中查询数组内的空日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325599/

相关文章:

Elasticsearch:没有在字段 [hostname] 上声明的类型 [keyword] 的处理程序

elasticsearch - 通过在Elasticsearch中部分匹配来搜索字符串数组

java - Camel 和 Elasticsearch 带屏蔽插件

MySQL 全文搜索 "AND" bool 模式到 ElasticSearch

regex - 在 fluentd 中使用正则表达式解析 json 数据时出错

elasticsearch - 如何在Elasticsearch中对执行进行基准测试?

Elasticsearch:elasticsearch.service 的作业失败

python - 比较大量二进制字符串

java - 如何设置日期格式以插入 Elasticsearch

elasticsearch - Elasticsearch通知