具有特定条件的嵌套对象上的弹性搜索查询

发布于 2025-02-04 18:22:23 字数 1830 浏览 3 评论 0原文

我需要一些有关弹性搜索查询的帮助。我的索引有一个名为部门的嵌套字段,假设嵌套对象具有dept-id-id字段。我想对这个嵌套对象进行搜索,以及下面我将要突出显示的条件。

Index/_mapping
{
    "items": {
        "mappings": {
            "properties": {
                "item-id": {"type": "text"},
                "departments": {
                    "type": "nested",
                    "properties": {
                        "dept-id": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

Sample Data
{
    "hits": [
        {
            "_index": "items",
            "_source": {
                "item-id": "item-1",
                "departments": [
                    {
                        "dept-id": "dept-1"
                    },
                    {
                        "dept-id": "dept-2"
                    },
                    {
                        "dept-id": "dept-3"
                    }
                ]
            }
        },
        {
            "_index": "items",
            "_source": {
                "item-id": "item-2",
                "departments": [
                    {
                        "dept-id": "dept-1"
                    }
                ]
            }
        },
        {
            "_index": "items",
            "_source": {
                "item-id": "item-3",
                "departments": [
                    {
                        "dept-id": "dept-2"
                    }
                ]
            }
        }
    ]
}

条件:

  • 我需要根据此无活动部门ID列表过滤上述数据。
    • 不活动的部门:[“ 1”]
  • 我正在尝试查询索引以使用item-id 1和3返回数据。
  • 我在嵌套对象上尝试了ass_not,但不是答案i' m寻找,因为它仅返回item-3。将活动字段(布尔类型)添加到嵌套对象中,这主要是解决我的问题的方法。但是,我很好奇是否有办法在没有其他字段的情况下查询此问题?

I need some help with the elastic search query. My index has a nested field called departments, let's say the nested object has a dept-id field. I would like to do a search on this nested object and the condition I will highlight below.

Index/_mapping
{
    "items": {
        "mappings": {
            "properties": {
                "item-id": {"type": "text"},
                "departments": {
                    "type": "nested",
                    "properties": {
                        "dept-id": {
                            "type": "text"
                        }
                    }
                }
            }
        }
    }
}

Sample Data
{
    "hits": [
        {
            "_index": "items",
            "_source": {
                "item-id": "item-1",
                "departments": [
                    {
                        "dept-id": "dept-1"
                    },
                    {
                        "dept-id": "dept-2"
                    },
                    {
                        "dept-id": "dept-3"
                    }
                ]
            }
        },
        {
            "_index": "items",
            "_source": {
                "item-id": "item-2",
                "departments": [
                    {
                        "dept-id": "dept-1"
                    }
                ]
            }
        },
        {
            "_index": "items",
            "_source": {
                "item-id": "item-3",
                "departments": [
                    {
                        "dept-id": "dept-2"
                    }
                ]
            }
        }
    ]
}

Condition:

  • I need to filter the data above based on this list of inactive-department ids.
    • inactive-department: ["1"]
  • I am trying to query for the index to return data with item-id 1 and 3.
  • I tried must_not on the nested object but it is not the answer I'm looking for as it only returns item-3. Adding an active field (boolean type) to the nested object which mostly will be a solution to my problem. However, I am curious is there a way to query this without an additional field?

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

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

发布评论

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

评论(1

安静 2025-02-11 18:22:23

我不知道我是否正确理解,但是您想通过“ dept-1”过滤(哪些是不活动的),并与文档返回:item-1和item-3?

我要做的第一件事是将Dept-ID字段的类型作为关键字。

我使用的查询是:

GET /teste/_search
{
  "query": {
    "nested": {
      "path": "departments",
      "query": {
        "bool": {
          "must_not": [
            {
              "terms": {
                "departments.dept-id": [
                  "dept-1"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

结果:

"hits" : [
      {
        "_index" : "teste",
        "_type" : "_doc",
        "_id" : "sJwaO4EBBR941NShfXGS",
        "_score" : 0.0,
        "_source" : {
          "item-id" : "item-3",
          "departments" : [
            {
              "dept-id" : "dept-2"
            }
          ]
        }
      },
      {
        "_index" : "teste",
        "_type" : "_doc",
        "_id" : "sZwaO4EBBR941NShpnH0",
        "_score" : 0.0,
        "_source" : {
          "item-id" : "item-1",
          "departments" : [
            {
              "dept-id" : "dept-1"
            },
            {
              "dept-id" : "dept-2"
            },
            {
              "dept-id" : "dept-3"
            }
          ]
        }
      }
    ]

I don't know if I understand correctly but you want to filter by "dept-1" (which are the inactive ones) and have the return with the docs: item-1 and item-3?

The first thing I did was put the type of the dept-id field as a keyword.

The query I used was this:

GET /teste/_search
{
  "query": {
    "nested": {
      "path": "departments",
      "query": {
        "bool": {
          "must_not": [
            {
              "terms": {
                "departments.dept-id": [
                  "dept-1"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Results:

"hits" : [
      {
        "_index" : "teste",
        "_type" : "_doc",
        "_id" : "sJwaO4EBBR941NShfXGS",
        "_score" : 0.0,
        "_source" : {
          "item-id" : "item-3",
          "departments" : [
            {
              "dept-id" : "dept-2"
            }
          ]
        }
      },
      {
        "_index" : "teste",
        "_type" : "_doc",
        "_id" : "sZwaO4EBBR941NShpnH0",
        "_score" : 0.0,
        "_source" : {
          "item-id" : "item-1",
          "departments" : [
            {
              "dept-id" : "dept-1"
            },
            {
              "dept-id" : "dept-2"
            },
            {
              "dept-id" : "dept-3"
            }
          ]
        }
      }
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文