【问题标题】:ElasticSearch Nested Query - exclude parent documentElasticSearch 嵌套查询 - 排除父文档
【发布时间】:2018-06-22 02:24:47
【问题描述】:

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

对于下面的示例,我试图排除其中一个嵌套作业具有current: true 并与company name: 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"
          }
     ...

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl elasticsearch-rails elasticsearch-ruby elasticsearch-nested


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 2018-05-10
      • 2013-01-08
      • 2017-10-22
      • 1970-01-01
      相关资源
      最近更新 更多