Elasticsearch - 获取嵌套字段

时间:2016-07-29 06:14:56

标签: elasticsearch

我想只获得嵌套字段,但不能,因为它不是叶字段。

GET index/_search
{
    "size": 10,
    "fields": [
       "nested_fields"
    ]
}

ERROR : "reason": "field [nested_fields] isn't a leaf field"

我在下面尝试但无法匹配嵌套对象中的每个id和名称。

GET index/_search
    {
        "size": 10,
        "fields": [
           "nested_fields.id",
           "nested_fields.name"
        ]
    }

结果:

"fields": {
               "events.id": [
                  "13342",
                  "24232",
                  "25534",
                  "63454"
               ],
               "events.name": [
                  "R1413",
                  "R1414",
                  "R1415",
                  "R1416",
               ]
            }

这是我的预期结果:

fields" : {
  "evets" : {
      "id" : "234234",
      "name" : "RP1524"
   },
    .... so on
}

1 个答案:

答案 0 :(得分:10)

如果你没有某个查询应该以某种方式匹配嵌套字段,你可以这样做:

GET /index/_search
{
  "size": 10,
  "_source": ["nested_fields.id", "nested_fields.name"]
}

如果您还有nested个查询,并且想要返回匹配的嵌套文档,则可以这样做(使用inner_hits):

{
  "query": {
    "nested": {
      "path": "nested_fields",
      "query": {"match_all": {}}, 
      "inner_hits": {}
    }
  }
}