Hello,World

能否在一个查询中 查询两个条件 在对两个结果进行除法计算

Elasticsearch | 作者 taoyantu | 发布于2019年03月02日 | 阅读数:6363

请教各位一个问题,我们有一个场景,想通过1个查询语句,计算两个查询结果的除法,比如,我有一个查询条件,用 idc: "BJ" 能统计出有100条数据符合要求 ,第二个条件 idc: "SH",能统计出有200个数据,我现在想要取到  100 / 200 这个值  50% 这个数据,请问能有办法实现吗?请各位大神指点一下。
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: liubin

#参考 es6.6版本


PUT test01_index/_doc/5
{
"x_value":15,
"y_value":3
}


POST test01_index/_doc/_search
{
"script_fields": {
"my_divide_field": {
"script": {
"lang": "expression",
"source": "doc['y_value'].value != 0 ? doc['x_value'].value / doc['y_value'].value : 0"
}
}
}
}

#fanhui返回
{
"took" : 97,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test01_index",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"fields" : {
"my_divide_field" : [
5.0
]
}
},
{
"_index" : "test01_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"fields" : {
"my_divide_field" : [
0.0
]
}
}
]
}
}

machuan11

赞同来自:

percentiles aggs 能满足你么.. 在query中过滤 idc:BJ 和 SH.  结果进行percentiles agg。。你可以试试. 如果不行请告知哈,我暂时没试验的环境

xiaoyanghapi - Elasticsearch 爱好者

赞同来自:

没大看懂问题。

要回复问题请先登录注册