ElasticSearch: access document nested value in groovy script

懵懂的女人 提交于 2019-12-04 11:47:06

问题


I have a document stored in ElasticSearch as below. _source:

 {
 "firstname": "John",
 "lastname": "Smith",
 "medals":[
           {
             "bucket": 100, 
             "count": 1
           },
           {
             "bucket": 150,
             "count": 2
           }
         ]
  }

I can access the string type value inside a document using doc.firstname for scripted metric aggregation http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html.

But I am not able to get the field value using doc.medals[0].bucket.

Can you please help me out and let me know how to access the values inside nested fields?


回答1:


Use _source for nested properties. Doc holds fields that are loaded in memory. Nested documents may not be loaded and should be accessed with _source.

For instance:

GET index/type
    {
     "aggs": {
      "NAME": {
      "scripted_metric": {
        "init_script": "_agg['collection']=[]",
        "map_script": "_agg['tr'].add(_source.propertry1.prop);",
        "combine_script": "return _agg",
        "reduce_script": "return _aggs"
      }
    }
  },
  "size": 0
}


来源:https://stackoverflow.com/questions/27936385/elasticsearch-access-document-nested-value-in-groovy-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!