首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ElasticSearch:如何使用多个过滤器过滤对象数组?

ElasticSearch:如何使用多个过滤器过滤对象数组?
EN

Stack Overflow用户
提问于 2022-10-14 15:17:54
回答 1查看 31关注 0票数 0
代码语言:javascript
复制
{
  "id": 9,
  "resolutions": [
    {
      "divisionId": 8
    }
  ]
},
{
  "id": 123,
  "resolutions": [
    {
      "employeeId": 1,
      "divisionId": 5
    },
    {
      "divisionId": 7
    }
  ]
}

索引由document对象组成,每个document都有一个带有对象的数组resolutionsresolution既可以给员工,也可以给部门。员工将同时拥有divisionIdemployeeId,但部门只有divisionId。我只需要为部门过滤。

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "should": [
                    {
                      "bool": {
                        "must": [
                          {
                            "nested": {
                              "path": "resolutions",
                              "query": {
                                "terms": {
                                  "resolutions.divisionId": [
                                    660
                                  ]
                                }
                              }
                            }
                          }
                        ],
                        "boost": 1
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "resolutions",
                              "query": {
                                "exists": {
                                  "field": "resolutions.employeeId"
                                }
                              }
                            }
                          }
                        ],
                        "boost": 1
                      }
                    }
                  ],
                  "minimum_should_match": 2,
                  "boost": 1
                }
              }
            ],
            "boost": 1
          }
        }
      ],
      "boost": 1
    }
  }
}

问题是,此查询检查解析数组的所有对象。因此,如果只将一个部门添加到数组中,我就会得到结果,但是如果我还添加了一个雇员,那么我就不会得到它。

如果数组中至少存在一个除法,而不管其他对象是什么,如何修复它以返回结果?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-15 05:19:20

你的问题很复杂。

下面将回答我们的查询

如果数组中至少存在一个除法,而不管其他对象是什么?

查询

代码语言:javascript
复制
{
  "query": {
    "nested": {
      "path": "resolutions",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "resolutions.employeeId"
              }
            }
          ],
          "filter": [
            {
              "exists": {
                "field": "resolutions.divisionId"
              }
            }
          ]
        }
      }
    }
  }
}

如果要对某些值进行筛选,可以使用术语查询替换筛选器中的存在条件。

如果要查找匹配的嵌套文档,请使用命中

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74071408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档