elasticsearch aggregation to sort by ratio of aggregations

这一生的挚爱 提交于 2019-12-07 11:56:49

问题


I have a scenario in analytics where I want to calculate least performing 20 outlets out of 1000+ outlets where performance = transactionCount/ VisitCount per month at an outlet.

Mappings are,

{
 "CustomerVisit": {
   "properties": {
     "outlet": {
       "type": "string",
       "index": "not_analyzed"
     },
     "customerName": {
       "type": "string",
       "index": "not_analyzed"
     },
     "visitMonth": {
       "type": "Date"
     },
    "visit": {
       "type": "nested",
       "properties": {
         "visitStatus": {
           "type": "long"
         },
         "transactionStatus": {
           "type": "long"
         },
         "remarks": {
           "type": "string",
           "index": "not_analyzed"
         }
       }
     }
   }
 }
}

Now, What I want is aggregate in such a way that gives me 20 outlets out of 1000+ sorted by ratio of aggregation of visitStatus to aggregation of transactionStatus.

I couldn't find a way to solve it using aggs, I don't know if script could be implemented here. The rough solution I was about to implement was

  1. aggregate by least transactionCount, get 20 buckets(with outlet names/counts) [one query executed]
  2. aggregate by visitCount for above 20 outlets [another query]

  3. divide count1 by count2 to get ratio

But this would never give me right result, because I'm sorting by least transactions not ratio of transactions/visits.

来源:https://stackoverflow.com/questions/24624371/elasticsearch-aggregation-to-sort-by-ratio-of-aggregations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!