聚合中使用脚本查询语句如下:
{
"aggregations" : {
"domain" : {
"terms" : {
"field" : "domain"
},
"aggregations" : {
"totalVist" : {
"sum" : {
"field" : "visit_count"
}
},
"totalHit" : {
"sum" : {
"field" : "hit_count"
}
},
"percentage" : {
"bucket_script" : {
"script" : {
"inline" : "tHit / tVisit"
},
"buckets_path" : {
"tVisit" : "totalVist",
"tHit" : "totalHit"
}
}
}
}
}
}
}
查询出得到的聚合结果如下:
"aggregations" : {
"domain" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "www.youku.com",
"doc_count" : 2,
"totalVist" : {
"value" : 300.0
},
"totalHit" : {
"value" : 150.0
},
"percentage" : {
"value" : 0.5
}
}, {
"key" : "www.jd.com",
"doc_count" : 1,
"totalVist" : {
"value" : 300.0
},
"totalHit" : {
"value" : 50.0
},
"percentage" : {
"value" : 0.16666666666666666
}
}, {
"key" : "www.qq.com",
"doc_count" : 1,
"totalVist" : {
"value" : 200.0
},
"totalHit" : {
"value" : 10.0
},
"percentage" : {
"value" : 0.05
}
}, {
"key" : "www.sina.com",
"doc_count" : 1,
"totalVist" : {
"value" : 200.0
},
"totalHit" : {
"value" : 50.0
},
"percentage" : {
"value" : 0.25
}
} ]
}
}
现在我想查询的时候执行percentage 值排序,es排序API好像都只能对字段进行排序,脚本查询出的结果无法排序。有大神对这个排序有研究的吗?还是es 只能对字段排序?
{
"aggregations" : {
"domain" : {
"terms" : {
"field" : "domain"
},
"aggregations" : {
"totalVist" : {
"sum" : {
"field" : "visit_count"
}
},
"totalHit" : {
"sum" : {
"field" : "hit_count"
}
},
"percentage" : {
"bucket_script" : {
"script" : {
"inline" : "tHit / tVisit"
},
"buckets_path" : {
"tVisit" : "totalVist",
"tHit" : "totalHit"
}
}
}
}
}
}
}
查询出得到的聚合结果如下:
"aggregations" : {
"domain" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "www.youku.com",
"doc_count" : 2,
"totalVist" : {
"value" : 300.0
},
"totalHit" : {
"value" : 150.0
},
"percentage" : {
"value" : 0.5
}
}, {
"key" : "www.jd.com",
"doc_count" : 1,
"totalVist" : {
"value" : 300.0
},
"totalHit" : {
"value" : 50.0
},
"percentage" : {
"value" : 0.16666666666666666
}
}, {
"key" : "www.qq.com",
"doc_count" : 1,
"totalVist" : {
"value" : 200.0
},
"totalHit" : {
"value" : 10.0
},
"percentage" : {
"value" : 0.05
}
}, {
"key" : "www.sina.com",
"doc_count" : 1,
"totalVist" : {
"value" : 200.0
},
"totalHit" : {
"value" : 50.0
},
"percentage" : {
"value" : 0.25
}
} ]
}
}
现在我想查询的时候执行percentage 值排序,es排序API好像都只能对字段进行排序,脚本查询出的结果无法排序。有大神对这个排序有研究的吗?还是es 只能对字段排序?
6 个回复
三斗室 - ELK
赞同来自:
qiqi18 - 80后攻城狮
赞同来自:
字段: domian(域名) visit_count(访问数) hit_count(连接成功数)
值: www.qq.com 100 50
www.qq.com 200 160
www.163.com 500 300
www.jd.com 800 740
我现在是要按域名统计访问成功率,按成功率进行排序。
三斗室 - ELK
赞同来自:
qiqi18 - 80后攻城狮
赞同来自:
"aggregations" : {
"domain" : {
"terms" : {
"field" : "domain"
},
"aggregations" : {
"totalVist" : {
"sum" : {
"field" : "visit_count"
}
},
"totalHit" : {
"sum" : {
"field" : "hit_count"
}
},
"percentage" : {
"bucket_script" : {
"script" : {
"inline" : "tHit / tVisit"
},
"buckets_path" : {
"tVisit" : "totalVist",
"tHit" : "totalHit"
}
}
}
}
}
}
}
官网没看到有说明Bucket Script Aggregation 的排序。
redhat
赞同来自:
zpzkit
赞同来自: