ES 使用order by script_metric 会有问题,官方文档中有order by 只能针对numbric,不能对pipeline,scripted_mertic排序
如下
POST biz_weibo_real_word_tblog_*/_search
{
"size" : 1,
"query" : {
"bool" : {
"must" : [
{
"term" : {
"word" : {
"value" : "范冰冰",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"version" : false,
"aggregations" : {
"agg_nested_rela_word0" : {
"nested" : {
"path" : "rela_word"
},
"aggregations" : {
"agg_b_terms_rela_word_word" : {
"terms" : {
"field" : "rela_word.word",
"size" : 20,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false
},
"aggregations" : {
"hot_value_" : {
"scripted_metric": {
"init_script": "params._agg.hv = 0.0;",
"map_script": "double factor=((params.now-doc['dt'].value)/params.day +1) ; params._agg.hv=(factor*doc['rela_word.hot_value'].value);",
"reduce_script": "double thv = 0; for (a in params._aggs) { thv += a.hv } return thv/params._aggs.size();",
"params": {
"field": "rela_word.hot_value,dt",
"_agg": {} ,
"now": 1531324800000,
"day": 3600000
}
}
}
}
}
}
}
}
}
POST biz_weibo_real_word_tblog_*/_search
{
"size" : 1,
"query" : {
"bool" : {
"must" : [
{
"term" : {
"word" : {
"value" : "范冰冰",
"boost" : 1.0
}
}
},
{
"term" : {
"dt" : {
"value" : "2018-07-12 00:00:00",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"version" : false,
"aggregations" : {
"agg_nested_rela_word0" : {
"nested" : {
"path" : "rela_word"
},
"aggregations" : {
"agg_b_terms_rela_word_word" : {
"terms" : {
"field" : "rela_word.word",
"size" : 20,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false,
"order": {
"hot_value_": "desc"
}
},
"aggregations" : {
"hot_value_" : {
"scripted_metric": {
"init_script": "params._agg.hv = 0.0;",
"map_script": "double factor= 0.0; if((params.now-doc['dt'].value)==0){factor=params.v_d;}else{ factor=params.v_d_; } params._agg.hv=(factor*doc['rela_word.hot_value'].value);",
"reduce_script": "double thv = 0; for (a in params._aggs) { thv += a.hv } return thv/params._aggs.size();",
"params": {
"field": "rela_word.hot_value,dt",
"_agg": {} ,
"now": 1531324800000,
"day": 3600000,
"v_d":0.8,
"v_d_":0.05
}
}
}
}
}
}
}
}
}
POST biz_weibo_real_word_tblog_*/_search
{
"size" : 1,
"query" : {
"bool" : {
"must" : [
{
"term" : {
"word" : {
"value" : "范冰冰",
"boost" : 1.0
}
}
},
{
"term" : {
"dt" : {
"value" : "2018-07-12 00:00:00",
"boost" : 1.0
}
}
}
],
"disable_coord" : false,
"adjust_pure_negative" : true,
"boost" : 1.0
}
},
"version" : false,
"aggregations" : {
"agg_nested_rela_word0" : {
"nested" : {
"path" : "rela_word"
},
"aggregations" : {
"agg_b_terms_rela_word_word" : {
"terms" : {
"field" : "rela_word.word",
"size" : 20,
"min_doc_count" : 1,
"shard_min_doc_count" : 0,
"show_term_doc_count_error" : false,
"order": {
"hot_value_": "desc"
}
},
"aggregations" : {
"hot_value_" : {
"sum": {
"script": "double factor= 0.0; long today=1531324800000L; if(doc['dt'].value==today){factor=0.8;}else{ factor=0.05; } return factor*doc['rela_word.hot_value'].value;"
}
}
}
}
}
}
}
}