我想要每个用户文档的平均评级,但不是根据me.please检查下面给出的代码。
curl -XGET 'localhost:9200/mentorz/users/_search?pretty' -H 'Content-Type: application/json' -d'
{"aggs" : {"avg_rating" : {"range" : {"field" : "rating","ranges" : [{ "from" : 3, "to" : 19 }]}}}}';{ "_index“:"mentorz","_type”:"users","_id“:"555","_source”:{ "name“:"neeru","user_id”:555,"email_id“:"abc@gmail.com","followers”:0,"following“:0,"mentors”:0,"mentees“:0,"basic_info”:“接口测试信息”,"birth_date“:1448451985397,"charge_price”:0,"org“:"cz","located_in”:"noida","position“:”软件开发者“,"exp”:7,"video_bio_lres“:”测试生物资源url正常注册“,"video_bio_hres”:“测试生物资源url正常注册”,“评级”:5 ,4,“专家”:1,4,61,62,63 }这是我的用户文档,我只想过滤那些平均评分范围在3到5之间的用户。
发布于 2017-09-12 13:37:13
更新应答
我已经用脚本做了一个查询,希望下面的查询能为你工作。
GET mentorz/users/_search
{
"size": 0,
"aggs": {
"term": {
"terms": {
"field": "user.keyword",
"size": 100
},
"aggs": {
"NAME": {
"terms": {
"field": "rating",
"size": 10,
"script": {
"inline": "float var=0;float count=0;for(int i = 0; i < params['_source']['rating'].size();i++){var=var+params['_source']['rating'][i];count++;} float avg = var/count; if(avg>=4 && avg<=5) {avg}else{null}"
}
}
}
}
}
}
}您可以通过更改if条件"if(avg>=4 && avg<=5)“来更改所需评级范围的范围。
https://stackoverflow.com/questions/46168030
复制相似问题