ElasticSearch 嵌套查询 - 排除父文档



尝试排除其中一个子文档与查询不匹配的顶级文档。

对于下面的示例,我尝试排除其嵌套作业之一具有current: true并与company name: Elastic匹配的所有文档。但是,由于其中一个嵌套的作业文档与current: false和公司name: Elastic匹配,因此返回此文档。我正在使用一个嵌套查询,其中必须与公司名称匹配,并且有一个过滤器,其中当前:false。我怎样才能使以下文件不被退回?

 "name": "john doe",
      "jobs": [
        {
          "title": "Vice President",
          "current": true,
          "company": {
            "name": "Elastic"
          }
        },
        {
          "title": "CEO",
          "current": false,
           "company": {
             "name": "Elastic"
          }
     ...

这个怎么样?请注意,我假设您有一个.keyword sub0字段,该字段基本上与大写字母完全匹配。如果字段名称不同,请相应地更改字段名称:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "jobs",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "jobs.current": {
                        "value": "true"
                      }
                    }
                  },
                  {
                    "term": {
                      "jobs.company.name.keyword": {
                        "value": "Elastic"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

最新更新