elasticsearch - 嵌套查询查找包含项X的子项和包含项Y的父项

标签 elasticsearch

我有这样的映射

{
    "properties" : {
        "text" : {
            "type" : "nested",
            "properties": {
                "header": {
                    "type" : "text"
                },
                "nodes": {
                    "type": "nested",
                    "properties": {
                        "subheader" : {
                            "type" : "text"
                        },
                        "nodes" : {
                            "type" : "nested"
                        }
                    }
                }
            }
        }
    }
}

我想返回的文档中,最里面的子text.nodes.nodes包含一些术语“X”,并且仅当该子项的父text.nodes.subheader包含一些术语“Y”时

这是一个reproducible gist,我希望在其中搜索子级中的“SUPERFRAGILISTIC”以及子标题中的“Restrictions”应仅返回文档2。
现在正在返回两个文档。

从要点来看,我的查询现在看起来像:
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "text.nodes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "text.nodes.subheader": {
                        "query": "Restrictions"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "text.nodes.nodes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "text.nodes.nodes.content": {
                        "query": "SUPERFRAGILISTIC"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

最佳答案

为了找到单个文档的相同json节点中包含两个条件的结果,您需要将第二个条件嵌套在第一个条件中。通过此查询,您将仅获得document2:

{
   "query":{
      "bool":{
         "must":[
            {
               "nested":{
                  "path":"text.nodes",
                  "query":{
                     "bool":{
                        "must":[
                           {
                              "match":{
                                 "text.nodes.subheader":{
                                    "query":"Restrictions"
                                 }
                              }
                           },
                           {
                              "nested":{
                                 "path":"text.nodes.nodes",
                                 "query":{
                                    "bool":{
                                       "must":[
                                          {
                                             "match":{
                                                "text.nodes.nodes.content":{
                                                   "query":"SUPERFRAGILISTIC"
                                                }
                                             }
                                          }
                                       ]
                                    }
                                 }
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

关于elasticsearch - 嵌套查询查找包含项X的子项和包含项Y的父项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218441/

相关文章:

elasticsearch - Elasticsearch 中的匹配数组

rdbms - 与 ElasticSearch 相比,使用 RDBMS 有何优势?

elasticsearch - OXID shop和ONGR如何建立连接?

elasticsearch - Elasticsearch 模糊查询奇怪的结果

elasticsearch - 模糊匹配失败,但精确匹配通过

elasticsearch - 加入/合并Elasticsearch结果

elasticsearch - 使用作业异步地从Elasticsearch删除数据

php - FOSElasticaBundle一起共享映射类型

elasticsearch - Logstash Logback编码器,Logstash转发器和Logstash

node.js - 如何在Node.js中添加多行无痛代码