我在索引中有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的用户才能工作,对于其他拥有更多角色的用户,它没有按预期工作,有人知道我应该改变什么才能得到我想要的查询吗?
2条答案
按热度按时间mm5n2pyu1#
您可以使用以下查询。它是相同的查询,但简化了。
字符串
测试数据,如果你需要:
型
jpfvwuh42#
我找到了正确的查询:
字符串
must_not应该在嵌套之前