首先定义一个索引的字段:
PUT /idx5 { "mappings":{ "properties": { "nodocvals": { "type": "long", "doc_values":false }, "yesdocval":{ "type":"long", "doc_values":true }, "cnt":{ "type":"long" } } } }
其中两个字段的doc_values参数一个设置为true,一个设置为false
然后添加文档:
PUT /idx5/_doc/1 { "nodocvals":1, "yesdocval":1, "cnt":1 }
然后执行聚合操作:
GET /idx5/_search { "aggs": { "grpfd": { "terms": { "field": "nodocvals" }, "aggs": { "avgfd": { "avg": { "field": "cnt" } } } } } }
用的聚合字段是nodocvals,它的doc_values定义为了false,然后就会报错:
"type" : "illegal_argument_exception", "reason" : "Can't load fielddata on [nodocvals] because fielddata is unsupported on fields of type [long]. Use doc values instead."
里面提到Use doc values instead,换成yesdocval字段来聚合,它的doc_values定义为了true,就不报错了:
GET /idx5/_search { "aggs": { "grpfd": { "terms": { "field": "yesdocval" }, "aggs": { "avgfd": { "avg": { "field": "cnt" } } } } } }
返回了结果:
... "grpfd" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 1, "doc_count" : 1, "avgfd" : { "value" : 1.0 } } ] } ...
加群入久伴博客官方微信群
加入久伴官方微信群有啥优势?
1.文章内部资源由于时间久远可能失效,加群,群内可以第一时间反馈
2.网站中教程不一定适用任何人,加群可以第一时间咨询并解决您得疑惑
3.你游荡于互联网,也需要群体,加群一起互动交流,畅所欲言
扫描下方二维码即可加入交流讨论
