Man*_*noj 3 subquery elasticsearch
我正在使用 ElasticSearch 5.6。我有以下 JSON 文档。
{
"cards": [{
"tag_categories": [
{
"is_sensitive": true,
"category": "Users",
"tags": [
{
"is_selected": true,
"name": "user1"
},
{
"is_selected": true,
"name": "user2"
},
{
"is_selected": false,
"name": "user3"
}
]
}
],
"risk": "medium",
"position": 1,
"placement": 4,
"title": "test title",
}, ...]
}
Run Code Online (Sandbox Code Playgroud)
如果所有给定的用户名和相应的 is_selected 值为 true,我想返回此文档。
这是我的查询。
{
"_source": {
"excludes": ["cards.pages"]
},
"query": {
"bool": {
"must": [{
"match": {
"_all": "hello world"
}
}],
"filter": [{
"nested": {
"path": "cards.tag_categories.tags",
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match": {
"cards.tag_categories.tags.name": "user2"
}
},
{
"match": {
"cards.tag_categories.tags.is_selected": true
}
}
]
}
},
{
"bool": {
"must": [
{
"match": {
"cards.tag_categories.tags.name": "user1"
}
},
{
"match": {
"cards.tag_categories.tags.is_selected": true
}
}
]
}
}
]
}
}
}
},
{
"term": {
"name.keyword": "hello name"
}
},
{
"term": {
"title.keyword": "hello title"
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了两个子布尔查询来匹配每组用户名和 is_selected 值。如果为真,父布尔查询将获取AND并返回文档。
在上面的示例查询中,当 user1 和 user2 匹配条件时,应返回文档。但这并没有发生。
如果我将单个用户与其 is_selected 值进行比较,文档将返回。例如:用户1。
如果有人能告诉我我在哪里犯了错误,我将不胜感激。
我添加了单独的嵌套块并且它起作用了!
{
"_source": {
"excludes": ["cards.pages"]
},
"query": {
"bool": {
"must": [{
"match": {
"_all": "hello world"
}
}],
"filter": [
{
"nested": {
"path": "cards.tag_categories.tags",
"query": {
"bool": {
"must": [
{
"term": {
"cards.tag_categories.tags.name.keyword": "user1"
}
},
{
"term": {
"cards.tag_categories.tags.is_selected": true
}
}
]
}
}
}
},
{
"nested": {
"path": "cards.tag_categories.tags",
"query": {
"bool": {
"must": [
{
"term": {
"cards.tag_categories.tags.name.keyword": "user2"
}
},
{
"term": {
"cards.tag_categories.tags.is_selected": true
}
}
]
}
}
}
},
{
"term": {
"name.keyword": "hello name"
}
},
{
"term": {
"title.keyword": "hello title"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
} }
| 归档时间: |
|
| 查看次数: |
12783 次 |
| 最近记录: |