ElasticSearch:以groovy脚本访问文档嵌套值 [英] ElasticSearch: access document nested value in groovy script
问题描述
我有一个存储在ElasticSearch中的文档如下。
_source:
I have a document stored in ElasticSearch as below. _source:
{
"firstname": "John",
"lastname": "Smith",
"medals":[
{
"bucket": 100,
"count": 1
},
{
"bucket": 150,
"count": 2
}
]
}
我可以使用 doc.firstname
访问文档中的字符串类型值,用于脚本度量标准聚合 http://www.elasticsearch.org/guide/ en / elasticsearch / reference / current / search-aggregations-metrics-scripted-metric-aggregation.html 。
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.
但是我无法获取字段价值使用 doc.medals [0] .bucket。
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?
推荐答案
使用_source进行嵌套属性。
Doc保存加载到内存中的字段。嵌套文档可能无法加载,并且可以使用_source访问。
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.
例如:
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
}
这篇关于ElasticSearch:以groovy脚本访问文档嵌套值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!