如何在弹性搜索中获取数组中所有对象都具有特定值的所有文档

发布于 2025-01-11 01:24:47 字数 857 浏览 0 评论 0 原文

假设我想获取数组字段中所有元素的“状态”不是未知的所有文档,

例如:

[
  {
    "type": "object1",
    "list": [
      {
        "node": "1",
        "status": "UP"
      },
      {
        "node": "2",
        "status": "DOWN"
      },
      {
        "node": "3",
        "status": "UNKNOWN"
      }
    ]
  },
  {
    "type": "object2",
    "list": [
      {
        "node": "1",
        "status": "UNKNOWN"
      },
      {
        "node": "2",
        "status": "UNKNOWN"
      }
    ]
  }
]

查询应仅返回“object1”文档,因为“object2”列表的所有元素均为未知。

映射,我已将其定义为嵌套对象,并且我已经可以搜索 list.status=UP 的记录,例如 只是想知道如何实现获取文档的用例,其中数组字段中的所有元素都不是某个值

尝试过这个

    {
  "query": {
    "bool" : {
      "must_not" : {
        "term" : { "list.status" : "UNKNOWN" }
      }
    }
  }
}

但是上面的查询在这种情况下不会返回object1(不是预期的),而是过滤掉object2(如预期的)

Lets assume I want to fetch all documents where "status" for all the elements in the array field is NOT UNKNOWN

For example:

[
  {
    "type": "object1",
    "list": [
      {
        "node": "1",
        "status": "UP"
      },
      {
        "node": "2",
        "status": "DOWN"
      },
      {
        "node": "3",
        "status": "UNKNOWN"
      }
    ]
  },
  {
    "type": "object2",
    "list": [
      {
        "node": "1",
        "status": "UNKNOWN"
      },
      {
        "node": "2",
        "status": "UNKNOWN"
      }
    ]
  }
]

And the query should return only "object1" document since "object2" list has all elements as UNKNOWN.

The mapping, I have defined it as a nested object and I can already search for records where list.status=UP for example
Just want to know how to achieve the use case of fetching documents where ALL the elements in the array field are NOT a certain value

Tried this

    {
  "query": {
    "bool" : {
      "must_not" : {
        "term" : { "list.status" : "UNKNOWN" }
      }
    }
  }
}

However the above query does not return object1 (not expected) in this case but does filter out object2 (as expected)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2025-01-18 01:24:47

您的问题是,即使嵌套文档之一匹配,也会返回完整的外部文档。您想要的是当所有嵌套文档匹配时,只有您想返回外部文档。

为此,文档建议must_not
尝试删除具有“未知”的文档,然后您将只得到“向上”和“向下”:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "list",
            "query": {
              "term": {
                "status": "UNKNOWN"
              }
            }
          }
        }
      ]
    }
  }
}

关于您的更新:您有相反的想法。对于嵌套字段,关联是完整的。对于数组字段的普通对象,关联会丢失。这就是嵌套类型存在的原因。 链接

Your problem is that even if one of the nested doc matches, the compete outer doc is returned. What you want is when all the nested docs match then only you want to return the outer doc.

For this, the documentation suggests must_not.
Try to remove the docs which have "UNKNOWN" and then you will get only with "UP" and "DOWN":

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "list",
            "query": {
              "term": {
                "status": "UNKNOWN"
              }
            }
          }
        }
      ]
    }
  }
}

Regarding you update: You have the opposite idea. With nested fields the association is intact. With a normal object of array field, the association is lost. That is why nested type exists. Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文