elasticsearch检索嵌套对象 - 而不是单个字段

时间:2015-05-13 14:25:57

标签: elasticsearch

当我使用"字段"查询的选项我为每个字段获得一个单独的数组。是否可以取回"完成"嵌套对象而不仅仅是字段?

在下面的示例中,如果我尝试"fields": ["cast"],它会告诉我,强制转换不是叶子节点。如果我"fields": ["cast.firstName", "cast.middleName", "cast.lastName"]它返回3个数组。

是否有其他方法只检索部分数量的文档?或者有没有办法重新组装"将单独的字段分成完整的"演员"对象

示例索引和数据:

POST /movies
{
   "mappings": {
      "movie": {
         "properties": {
            "cast": {
               "type": "nested"
            }
         }
      }
   }
}

POST /movies/movie
{
   "title": "The Matrix",
   "cast": [
      {
         "firstName": "Keanu",
         "lastName": "Reeves",
         "address": {
            "street": "somewhere",
            "city": "LA"
         }
      },
      {
         "firstName": "Laurence",
         "middleName": "John",
         "lastName": "Fishburne",
         "address": {
            "street": "somewhere else",
            "city": "NYC"
         }
      }
   ]
}

示例查询:

GET /movies/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "cast",
               "filter": {
                  "bool": {
                     "must": [
                        { "term": { "firstName": "laurence"} },
                        { "term": { "lastName": "fishburne"} }
                     ]
                  }
               }
            }
         }
      }
   },
   "fields": [
      "cast.address.city",
      "cast.firstName",
      "cast.middleName",
      "cast.lastName"
   ]
}

示例查询的结果:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "movies",
            "_type": "movie",
            "_id": "AU1JeyBseLgwMCOuOLsZ",
            "_score": 1,
            "fields": {
               "cast.firstName": [
                  "Keanu",
                  "Laurence"
               ],
               "cast.lastName": [
                  "Reeves",
                  "Fishburne"
               ],
               "cast.address.city": [
                  "LA",
                  "NYC"
               ],
               "cast.middleName": [
                  "John"
               ]
            }
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:5)

我认为这是你正在寻找的东西:

POST /movies/_search
{
  "_source": {
    "include": [
      "cast.address.city",
      "cast.firstName",
      "cast.middleName",
      "cast.lastName"
    ]
  },
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "path": "cast",
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "firstName": "laurence"
                  }
                },
                {
                  "term": {
                    "lastName": "fishburne"
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

结果:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "movies",
            "_type": "movie",
            "_id": "AU1PIJgBA_0Cyshym7-m",
            "_score": 1,
            "_source": {
               "cast": [
                  {
                     "lastName": "Reeves",
                     "address": {
                        "city": "LA"
                     },
                     "firstName": "Keanu"
                  },
                  {
                     "middleName": "John",
                     "lastName": "Fishburne",
                     "address": {
                        "city": "NYC"
                     },
                     "firstName": "Laurence"
                  }
               ]
            }
         }
      ]
   }
}

您也可以选择排除字段而不是包含字段或两者,请参阅此处的文档:http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html