ElasticSearch嵌套查询-排除父文档 [英] ElasticSearch Nested Query - exclude parent document
问题描述
尝试排除其中一个子文档与查询不匹配的顶级文档.
Trying to exclude top-level documents where one of the child documents doesn't match the query.
对于下面的示例,我试图排除其嵌套作业之一具有 current:true
且与公司名称:Elastic
匹配的所有文档.但是,由于其中一个嵌套的作业文档与 current:false
和公司 name:Elastic
相匹配,因此将返回此文档.我使用的嵌套查询的公司名称必须与之匹配,且当前位置为false的过滤器.如何做到不退回以下文件?
For the example below, I'm trying to exclude all documents where one of its nested jobs has current: true
, and matches with the company name: Elastic
. But since one of the nested job documents matches with current: false
and company name: Elastic
, this document is returned. I am using a nested query with a must match on company name and a filter where current: false. How can I make it so that the below document is not returned?
"name": "john doe",
"jobs": [
{
"title": "Vice President",
"current": true,
"company": {
"name": "Elastic"
}
},
{
"title": "CEO",
"current": false,
"company": {
"name": "Elastic"
}
...
推荐答案
这个怎么样?请注意,我假设您有一个 .keyword
子0字段,该字段基本上与大写字母完全匹配.如果您的名称有所不同,请相应地更改字段名称:
How about this one? Note that I assumed you have a .keyword
sub0field that is basically matching exactly on the upper case letter. If you have it differently, change the field name accordingly:
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "jobs",
"query": {
"bool": {
"must": [
{
"term": {
"jobs.current": {
"value": "true"
}
}
},
{
"term": {
"jobs.company.name.keyword": {
"value": "Elastic"
}
}
}
]
}
}
}
}
]
}
}
}
这篇关于ElasticSearch嵌套查询-排除父文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!