具有多个嵌套属性的映射中的嵌套查询

时间:2015-04-20 17:44:37

标签: elasticsearch nested

我有一个索引,其中包含以下映射:

curl -XPUT "http://localhost:9200/index1" -d'
{
    "mappings": {
        "movie": {
            "properties": {
                "people": {
                    "properties": {
                        "cast": {
                            "type": "nested",
                            "properties": {
                                "firstName": {
                                    "type": "string"
                                },
                                "lastName": {
                                    "type": "string"
                                }
                            }
                        },
                        "producers": {
                           "type": "nested",
                           "properties": {
                              "lastName": {
                                 "type": "string"
                              }
                           }
                        }
                    }
                }
            }
        }
    }
}'

然后我在那里放了一个简单的文档:

curl -XPOST "http://localhost:9200/index1/movie/" -d'
{
   "title": "The Matrix",
   "people": {
        "cast": [
      {
         "firstName": "Keanu",
         "lastName": "Reeves"
      },
      {
         "firstName": "Laurence",
         "lastName": "Fishburne"
      }
    ]
    }
}'

现在,这是有趣的部分。如果我为firstName制作嵌套过滤器 这场比赛,它很好。但是,如果我为lastName执行此操作,则无效 一点都不似乎因为这个字段出现在两个嵌套属性中我不能 用它们过滤:

WORKS:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "firstName": "keanu"
                  }
               }
            }
         }
      }
   }
}'

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"index1","_type":"movie","_id":"AUzX6wLHdI5sfxoODE5E","_score":1.0,"_source":
{
   "title": "The Matrix",
   "people": {
    "cast": [
      {
         "firstName": "Keanu",
         "lastName": "Reeves"
      },
      {
         "firstName": "Laurence",
         "lastName": "Fishburne"
      }
    ]
  }
}}]}}

DOESN'工作:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "lastName": "reeves"
                  }
               }
            }
         }
      }
   }
}'

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%  

这是一个错误还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

我真的不明白为什么你的第一个查询有效,但是如何让第二个查询工作:

curl -XPOST "http://localhost:9200/index1/movie/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "people.cast",
               "filter": {
                  "term": {
                     "people.cast.lastName": "reeves"
                  }
               }
            }
         }
      }
   }
}'

以下是我用来测试它的代码:

http://sense.qbox.io/gist/fd9c45768ae441cd08e6754f41e0a7df91fb758e