Elasticsearch -使用must_not的嵌套查询不能按预期工作

pu3pd22g  于 7个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(68)

我在索引中有2个用户,每个用户都有嵌套的角色:
用户ID=1

{
    "id": [1]
}

字符串
用户ID=2

{
    "id": [1]
},
{
    "id": [2]
},
{
    "id": [3]
}


用户ID=3

{
    "id": [2]
},
{
    "id": [3]
}


用户ID=4

{
    "id": [3]
}


ID=1的第一个用户具有ID=1的角色ID=2的第二个用户具有ID= 1、2、3的角色ID=3的第三个用户具有ID= 2、3的角色ID=4的第四个用户具有ID=3的角色
在第一个查询中,我试图获取ID=3的所有角色的用户,并且该查询正在工作:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "path": "roles",
                                    "query": {
                                        "bool": {
                                            "must": [
                                                {
                                                    "match": {
                                                        "roles.id": 3
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}


查询按预期工作,返回ID= 2,3,4的用户
但是当我尝试获取所有没有ID=3的角色的用户时(之前查询中的must改为must_not),它并没有像我预期的那样工作,我希望只有ID=1的用户会被返回,因为他没有ID=3的角色,而不是返回ID= 1,2,3的用户:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "path": "roles",
                                    "query": {
                                        "bool": {
                                            "must_not": [
                                                {
                                                    "match": {
                                                        "roles.id": 3
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}


只有只有角色must_not的用户才能工作,对于其他拥有更多角色的用户,它没有按预期工作,有人知道我应该改变什么才能得到我想要的查询吗?

mm5n2pyu

mm5n2pyu1#

您可以使用以下查询。它是相同的查询,但简化了。

GET test_index_nested/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "roles",
            "query": {
              "match": {
                "roles.id": 3
              }
            }
          }
        }
      ]
    }
  }
}

字符串
测试数据,如果你需要:

PUT test_index_nested
{
  "mappings": {
    "properties": {
      "roles": {
        "type": "nested"
      }
    }
  }
}

PUT test_index_nested/_doc/1?pretty
{
  "roles" : [
    {
      "id" : "1"
    },
    {
      "id" : "2"
    }
  ]
}

PUT test_index_nested/_doc/2?pretty
{
  "roles" : [
    {
      "id" : "3"
    }
  ]
}

jpfvwuh4

jpfvwuh42#

我找到了正确的查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must_not": [
                            {
                                "nested": {
                                    "path": "roles",
                                    "query": {
                                        "bool": {
                                            "should": [
                                                {
                                                    "match": {
                                                        "roles.id": 3
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

字符串
must_not应该在嵌套之前

相关问题