尝试排除其中一个子文档与查询不匹配的顶级文档。
对于下面的示例,我尝试排除其嵌套作业之一具有 current: true
且与公司名称:Elastic
匹配的所有文档。但由于嵌套作业文档之一与 current: false
和公司 name: Elastic
匹配,因此返回该文档。我正在使用嵌套查询,该查询必须匹配公司名称和过滤器,其中 current: false。我怎样才能不返回以下文档?
"name": "john doe",
"jobs": [
{
"title": "Vice President",
"current": true,
"company": {
"name": "Elastic"
}
},
{
"title": "CEO",
"current": false,
"company": {
"name": "Elastic"
}
...
最佳答案
这个怎么样? 请注意,我假设您有一个 .keyword
子字段,它基本上与大写字母完全匹配。如果您有不同的情况,请相应地更改字段名称:
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "jobs",
"query": {
"bool": {
"must": [
{
"term": {
"jobs.current": {
"value": "true"
}
}
},
{
"term": {
"jobs.company.name.keyword": {
"value": "Elastic"
}
}
}
]
}
}
}
}
]
}
}
}
关于ElasticSearch 嵌套查询 - 排除父文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229388/