keyword
返回的数据类型为org.elasticsearch.index.fielddata.ScriptDocValues
,继承AbstractList
是一个数组。常用api有getValue()
获取数组第一个值;get(int index)
获取数值第x个值。
ScriptDocValues
Modifier and Type |
Class and Description |
static class |
ScriptDocValues.Booleans |
static class |
ScriptDocValues.BytesRefs |
static class |
ScriptDocValues.Dates |
static class |
ScriptDocValues.Doubles |
static class |
ScriptDocValues.GeoPoints |
static class |
ScriptDocValues.Longs |
static class |
ScriptDocValues.Strings |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| GET /user_profile_v2/_search { "size": 0, "query": { "bool": { "must": [{ "match": { "merchant_code.keyword": "jt20180528102410333649" } }] } }, "aggs": { "birth_month_count": { "scripted_metric": { "init_script" : "params._agg.transactions = 0", "map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag){params._agg.transactions++}", "combine_script" : "return params._agg.transactions", "reduce_script" : "int result = 0;for(t in params._aggs) {result += t} return result" } }, "birth_month_list": { "scripted_metric": { "init_script" : "params._agg.transactions = []", "map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag && !params._agg.transactions.contains(value)){params._agg.transactions.add(value)}", "combine_script" : "return params._agg.transactions", "reduce_script" : "List result = []; for (int i=0;i<params._aggs.length;i++) {for (t in params._aggs[i]){if(!result.contains(t)){result.add(t)}}} return result" } }, "birth_month_map": { "scripted_metric": { "init_script" : "params._agg.transactions = [:]", "map_script" : "String value = (doc['birth_month.keyword']==null?'':doc['birth_month.keyword'].getValue());boolean flag = value !='';if(flag && !params._agg.transactions.containsKey(value)){params._agg.transactions.put(doc['card_no.keyword'].getValue(),value)}", "combine_script" : "return params._agg.transactions", "reduce_script" : "Map result = [:]; for (int i=0;i<params._aggs.length;i++) {for (key in params._aggs[i].keySet()){if(!result.containsKey(key)){result.put(key,params._aggs[i].get(key))}}} return result" } } } }
|
更新
基于script的统计速度慢,对于count
场景的统计直接使用批量查询替代,如下:
1 2 3 4 5
| POST /_msearch?rest_total_hits_as_int=true {"index":"user_profile_v2"} {"size":0,"query":{"bool":{"must":[{"term":{"merchant_code":{"value":"jt20191017175015910985"}}}],"should":[{"terms":{"fans_type.keyword":["公众号粉丝"]}}],"minimum_should_match":1}}} {"index":"user_profile_v2"} {"size":0,"query":{"bool":{"must":[{"term":{"merchant_code":{"value":"jt20191017175015910985"}}}],"should":[{"terms":{"app_scope.keyword":["会员"]}}],"minimum_should_match":1}}}
|