Elasticsearch range with aggs -
i want average rating of every user document not working according me.please check code given below.
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" : "api test info", "birth_date" : 1448451985397,"charge_price" : 0,"org" : "cz","located_in" : "noida", "position" : "sw developer", "exp" : 7, "video_bio_lres" : "test bio lres url normal signup","video_bio_hres" : "test bio hres url normal signup", "rating" : [ 5 ,4], "expertises" : [ 1, 4, 61, 62, 63 ] } user document,i want filter users have average rating range 3 5.
update answer
i've made query using script, hope below query works you.
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 = 0; < params['_source']['rating'].size();i++){var=var+params['_source']['rating'][i];count++;} float avg = var/count; if(avg>=4 && avg<=5) {avg}else{null}" } } } } } } }
you can change range of desired rating range changing if condition "if(avg>=4 && avg<=5)".
Comments
Post a Comment