Using Dynamic variables in aggregate query from Vega (in Kibana) to ElasticSearch

Viewed 438

We have a report that fetches data from elasticsearch and displays them as buckets. We want to show a band, customers and profit amount for each band. The idea is that we should give a band size parameter and then Vega should create 5 buckets for that when querying the data. If I mention the band size to be 50, then vega will pass this on in the query and create buckets 0-50, 51 to 100, 101 to 150, 151 to 200 and 200+. I am really struggling with this a lot and can not make it work. I am pasting the vega script here that works with hard coded values but when it comes to doing it through signals, I have not reached any clue at all. I would like to use the value from signal to define to band parameter. Please help.

{
   "$schema":"https://vega.github.io/schema/vega/v4.json",
   "data":[
      {
         "name":"SalesData",
         "url":{
            "index":"orderaggregates",
            "body":{
               "size":0,
               "aggs":{
                  "bands":{
                     "range":{
                        "field":"total_sales",
                        "ranges":[
                           {
                              "from":0,
                              "to":50
                           },
                           {
                              "from":50.01,
                              "to":100
                           },
                           {
                              "from":100.01,
                              "to":150
                           },
                           {
                              "from":150.01,
                              "to":200
                           },
                           {
                              "from":200.01
                           }
                        ]
                     },
                     "aggs":{
                        "total_sales":{
                           "sum":{
                              "field":"total_sales"
                           }
                        },
                        "customers":{
                           "cardinality":{
                              "field":"user_id"
                           }
                        },
                        "confirmed_orders":{
                           "sum":{
                              "field":"confirmedorders"
                           }
                        },
                        "cost_total":{
                           "sum":{
                              "field":"cost_total"
                           }
                        },
                        "shipping_collected":{
                           "sum":{
                              "field":"freightcollected"
                           }
                        },
                        "shipping_paid":{
                           "sum":{
                              "field":"freight_paid"
                           }
                        }
                     }
                  }
               }
            }
         },
         "format":{
            "property":"aggregations.bands.buckets"
         },
         "transform":[
            {
               "type":"project",
               "fields":[
                  "key",
                  "doc_count",
                  "customers.value",
                  "cost_total.value",
                  "total_sales.value"
               ],
               "as":[
                  "range",
                  "orders",
                  "customers",
                  "costs",
                  "sales"
               ]
            },
            {
               "type":"identifier",
               "as":"id"
            },
            {
               "type":"formula",
               "as":"y",
               "expr":"datum.id * 30"
            },
            {
               "type":"formula",
               "as":"orders",
               "expr":"format(datum.orders,',.0f')"
            },
            {
               "type":"formula",
               "as":"customers",
               "expr":"format(datum.customers,',.0f')"
            },
            {
               "type":"formula",
               "as":"costs",
               "expr":"format(datum.costs,'$,.0f')"
            },
            {
               "type":"formula",
               "as":"sales",
               "expr":"format(datum.sales,'$,.0f')"
            }
         ]
      }
   ],
   "signals":[
      {
         "name":"Group",
         "value":40,
         "bind":{
            "input":"range",
            "min":10,
            "max":100,
            "step":10
         }
      },
      {
         "name":"GroupSize",
         "update":"Group/2"
      }
   ],
   "marks":[
      {
         "from":{
            "data":"SalesData"
         },
         "type":"text",
         "encode":{
            "enter":{
               "fill":{
                  "value":"#000"
               },
               "text":{
                  "field":"range"
               },
               "x":{
                  "value":50
               },
               "y":{
                  "field":"y"
               }
            },
            "update":{
               "opacity":{
                  "value":1
               },
               "fontSize":{
                  "signal":"GroupSize"
               }
            },
            "hover":{
               "opacity":{
                  "value":0.5
               }
            }
         }
      },
      {
         "from":{
            "data":"SalesData"
         },
         "type":"text",
         "encode":{
            "enter":{
               "fill":{
                  "value":"#000"
               },
               "text":{
                  "field":"customers"
               },
               "x":{
                  "value":250
               },
               "y":{
                  "field":"y"
               }
            },
            "update":{
               "opacity":{
                  "value":1
               },
               "fontSize":{
                  "signal":"GroupSize"
               }
            },
            "hover":{
               "opacity":{
                  "value":0.5
               }
            }
         }
      },
      {
         "from":{
            "data":"SalesData"
         },
         "type":"text",
         "encode":{
            "enter":{
               "fill":{
                  "value":"#000"
               },
               "text":{
                  "field":"orders"
               },
               "x":{
                  "value":350
               },
               "y":{
                  "field":"y"
               }
            },
            "update":{
               "opacity":{
                  "value":1
               },
               "fontSize":{
                  "signal":"GroupSize"
               }
            },
            "hover":{
               "opacity":{
                  "value":0.5
               }
            }
         }
      },
      {
         "from":{
            "data":"SalesData"
         },
         "type":"text",
         "encode":{
            "enter":{
               "fill":{
                  "value":"#000"
               },
               "text":{
                  "field":"sales"
               },
               "x":{
                  "value":450
               },
               "y":{
                  "field":"y"
               }
            },
            "update":{
               "opacity":{
                  "value":1
               },
               "fontSize":{
                  "signal":"GroupSize"
               }
            },
            "hover":{
               "opacity":{
                  "value":0.5
               }
            }
         }
      }
   ]
}
1 Answers
Related