Aggregation on Vega-LIte

Hi

I'm trying to build an histogram chart with aggregations by vega-lite.

The aggregation is:

"aggs": {
  "gate": {
    "terms": {
      "field": "host.abbr.keyword",
      "order": {
        "dcam": "desc"
      },
      "size": 20
    },
    "aggs": {
      "dcam": {
        "sum": {
          "field": "msg.dcamera_perc_time"
        }
      },
      "difft": {
        "sum": {
          "field": "msg.difftime"
        }
      },
      "dcamtime": {
        "bucket_script": {
          "buckets_path": {
            "dc": "dcam",
            "dt": "difft"
          },
          "script": "Math.round((params.dc / params.dt) * 100) / 100.0"
        }
      }
    }
  }
}

I put it in a standard query and it works!

Now I can't insert it into a vega-lite code to build an histogram chart. This is the vega-lite code:

{
  $schema: https://vega.github.io/schema/vega-lite/v2.json
  title: title
  // Define the data source
  data: {
    url: {
      // Apply dashboard context filters when set
      %context%: true
      // Filter the time picker (upper right corner) with this field
      %timefield%: "msg.timestamp_lastmsg"

      index: my_index*

      body: {
        "aggs": {
          "gate": {
            "terms": {
              "field": "host.abbr.keyword",
              "order": {
                "dcam": "desc"
              },
              "size": 20
            },
            "aggs": {
              "dcam": {
                "sum": {
                  "field": "msg.dcamera_perc_time"
                }
              },
              "difft": {
                "sum": {
                  "field": "msg.difftime"
                }
              },
              "dcamtime": {
                "bucket_script": {
                  "buckets_path": {
                    "dc": "dcam",
                    "dt": "difft"
                  },
                  "script": "Math.round((params.dc / params.dt) * 100) / 100.0"
                }
              }
            }
          }
        }
      }
    }
    format: {property: "aggregations.gate.buckets"}
  }
  mark: {type: "bar"}
  encoding: {
    x: {
      field: key
      type: ordinal
      axis: {title: "Gate"}
      sort: {op: "sum", field: "dcamtime", order: "descending"}
    }
    y: {
      field: dcamtime
      type: quantitative
      axis: {title: "Dcamera"}
      "scale": {"domain": [0,100]}
    }
    color: {
      type: quantitative
      field: dcamtime
      title: "Dcam"
      scale: {
        "domain": [0,100]
        "range": ["darkgreen", "green", "yellow", "orange", "red", "darkred", "black"]
      }
    } 
  }
}

There's no grammar errors, but chart is empty with the right values in the X axis (gate field).

What am I doing wrong?
Thank you in advance
Giacomo

Hi @gpolini

Vega works directly with the data format returned by the ES query, if you take a look at the output of your search on your browser dev tools or copying and pasting your query on the Kibana dev tools, you will see that your aggregation will looks like:

"buckets" : [
        {
          "key" : "CN",
          "doc_count" : 2621,
          "dcam" : {
            "value" : 1.569872E7
          },
          "difft" : {
            "value" : 1.569872E7
          },
          "dcamtime" : {
            "value" : 1.0
          }
        },

Using format: {property: "aggregations.gate.buckets"} you are basically specifying that your data array reside in the nested object aggregations.gate.buckets of the ES response.

To use the data in this case you have to refer exactly to the key where the data is stored, Vega cannot extract automatically a value out of an object like "dcamtime" : { "value" : 1.0}, you have to specify manually that path like field: "dcamtime.value"

1 Like

Thank you so much @markov00 !

Now it works!!

Thank you, Giacomo

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.