多级嵌套对象中的弹性搜索

时间:2016-05-19 07:09:34

标签: php elasticsearch

我有下面的映射,我试图查询内部最高级别的嵌套元素,但它给了我错误。

映射:

{
  "search_v1" : {
    "mappings" : {
      "statement" : {
        "properties" : {
          "details" : {
            "type" : "text",            
          },
          "source" : {
            "type" : "nested",
            "properties" : {
              "affiliation" : {
                "type" : "nested",
                "properties" : {                 
                  "name" : {
                    "type" : "text"                   
                  }
                }
              },
              "first_name" : {
                "type" : "text"
              },
              "last_name" : {
                "type" : "text"
              }
            }
          }
        }
      }
    }
  }
}

数据:

{
  "_index" : "search_v1",
  "_type" : "statement",
  "_id" : "AVTHp5y8yr47EGbDlTWu",
  "_score" : 1.0,
  "_source" : {
    "details" : "test test",
    "source" : {          
      "first_name" : "source2",
      "last_name" : "source2",
      "affiliation" : [ {            
        "name" : "affiliation1"
      }, {            
        "name" : "affiliation2"
      }, {            
        "name" : "affiliation4"
      } ]
    }
  }
}

查询:

{
  "query": {
    "bool": {
      "must": [       
        {
          "nested": {
            "path": "source", 
            "query": {
              "bool": {
                "must": [ {
                    "nested": {
                      "path": "affiliation", 
                      "query": {
                        "bool": {
                          "must": [ 
                            { "match": { "affiliation.name": "affiliation2" }}
                          ]
                  }}}}
                ]
        }}}}
      ]
}}}

错误: query_shard_exception:无法创建查询

如果我尝试查询source.first_name然后它可以工作,但当我尝试深入1级时会出错。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试此查询。我认为您不需要双嵌套查询,但您确实需要匹配查询中的完全限定路径。

{
   "query": {
      "bool": {
         "must": [
            {
               "nested": {
                  "path": "source",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "source.affiliation.name": "affiliation2"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}