如何在嵌套字段

时间:2017-01-10 05:23:42

标签: elasticsearch

示例文档

{
 "id" : "video1",
  "title" : "Gone with the wind",
  "timedTextLines" : [ 
    {
      "startTime" : "00:00:02",
      "endTime" :  "00:00:05",
      "textLine" : "Frankly my dear I don't give a damn."
    },
   {
      "startTime" : "00:00:07",
      "endTime" :  "00:00:09",
      "textLine" : " my amazing country."
    },
 {
      "startTime" : "00:00:17",
      "endTime" :  "00:00:29",
      "textLine" : " amazing country."
    }
  ]
}

索引定义

{
  "mappings": {
    "video_type": {
      "properties": {
        "timedTextLines": {
          "type": "nested" 
        }
      }
    }
  }
}

内部工作中没有源过滤的响应很好。

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.91737854,
    "hits": [
      {
        "_index": "video_index",
        "_type": "video_type",
        "_id": "1",
        "_score": 0.91737854,
        "_source": {

        },
        "inner_hits": {
          "timedTextLines": {
            "hits": {
              "total": 1,
              "max_score": 0.6296964,
              "hits": [
                {
                  "_nested": {
                    "field": "timedTextLines",
                    "offset": 0
                  },
                  "_score": 0.6296964,
                  "_source": {
                    "startTime": "00:00:02",
                    "endTime": "00:00:05",
                    "textLine": "Frankly my dear I don't give a damn."
                  },
                  "highlight": {
                    "timedTextLines.textLine": [
                      "Frankly my dear I don't give a <em>damn</em>."
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

Response包含嵌套属性的所有属性。即startTime,endTime和textLine。如何在响应中仅返回endtime和startTime?

查询失败

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "gone"
          }
        },
        {
          "nested": {
            "path": "timedTextLines",
            "query": {
              "match": {
                "timedTextLines.textLine": "damn"
              }
            },
            "inner_hits": {
             "_source":["startTime","endTime"],
              "highlight": {
                "fields": {
                  "timedTextLines.textLine": {

                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "_source":"false"
}

错误 HTTP / 1.1 400错误请求 content-type:application / json;字符集= UTF-8 内容长度:265

  

{&#34;错误&#34; {&#34; ROOT_CAUSE&#34;:[{&#34;类型&#34;:&#34; illegal_argument_exception&#34;&#34;理由&#34 ;:&#34; [inner_hits]   _source不支持类型的值:START_ARRAY&#34;}],&#34;输入&#34;:&#34; illegal_argument_exception&#34;,&#34; reason&#34;:&#34; [ inner_hits]   _source不支持类型值:START_ARRAY&#34;},&#34; status&#34;:400}

1 个答案:

答案 0 :(得分:4)

原因是因为ES 5.0 _source中的inner_hits不再支持短格式,而只支持完整的对象格式(includesexcludes )(see this open issue

您的查询可以像这样重写,并且可以正常运行:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "gone"
          }
        },
        {
          "nested": {
            "path": "timedTextLines",
            "query": {
              "match": {
                "timedTextLines.textLine": "damn"
              }
            },
            "inner_hits": {
             "_source": {
                "includes":[
                  "timedTextLines.startTime",
                  "timedTextLines.endTime"
                ]
             },
              "highlight": {
                "fields": {
                  "timedTextLines.textLine": {

                  }
                }
              }
            }
          }
        }
      ]
    }
  },
  "_source":"false"
}