# Problem date\_histogram in vega with aggregation

**URL:** <https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487>\
**Category:** Kibana\
**Created:** [April 17, 2020, 10:02am UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487 "2020-04-17T10:02:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nb03briceno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nb03briceno/32/45777_2.png) [@nb03briceno](https://discuss.elastic.co/u/nb03briceno)\
**Post date:** [April 17, 2020, 10:02am UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/1 "2020-04-17T10:02:05Z")

</div>

Hello everyone,

I'm trying to create a graph with vega. A date histogram graph from an aggregation, but it is not working. In Kibana, it shows me the following error.

\_.aggregations is undefined

Would you mind to make a look, and giving to me a suggestion.

The associated code is this:

{  
$schema: [https://vega.github.io/schema/vega-lite/v2.json](https://vega.github.io/schema/vega-lite/v2.json)  
title: Daily Count

data: {  
url: {

```
  index: hs_index
  
  
   body: {
    
	size: 1000000,
	// Just ask for the fields we actually need for visualization
    _source: ["@timestamp","@version","consumed","consumed_text1","consumed_text2","free","free_text1","free_text2","host", "logLevel", "logdate","max","max_text1","max_text2","message","values","values_names","date_time","date_time","message"]
  	   
	"track_total_hits": true,
	"query": {
		"match": {
			"message": "COMMAND ORDER"
		}	
	}, 
	"aggregations":{
			order_over_time:{
				date_histogram: {
					field: "date_time",
					format: "yyyy-MM-dd hh:mm-ss",
					interval: "day"
				}
			}
	}

 }
}
format: {property: "aggregations.order_over_time.buckets"}

```

}

mark: bar

encoding: {  
x: {  
field: key  
type: temporal  
axis: {title: false}  
}  
y: {  
field: doc\_count  
type: quantitative  
axis: {title: "Call count"}  
}  
}  
}

Thanks so much,

JUAN DAVID BRICENO GUERRERO

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 17, 2020, 10:33am UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/2 "2020-04-17T10:33:23Z")

</div>

Few comments:

- Please edit the question to be formatted properly, wrapping the code in ``` (see Markdown code sections)
- Using `size: 1000000` will probably make the request fail and if you're interested only on aggregations, you can set it to `0`
- Why do you want to use Vega for a histogram aggregation when you have Kibana native histogram aggregations?

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 17, 2020, 10:42am UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/3 "2020-04-17T10:42:32Z")

</div>

Try with the following:

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "title": "Daily Count",
  "data": {
    "url": {
      "index": "hs_index",
      "body": {
        "size": 0,
        "track_total_hits": true,
        "query": {"match_all": {}},
        "aggregations": {
          "order_over_time": {
            "date_histogram": {
              "field": "timestamp",
              "format": "yyyy-MM-dd hh:mm-ss",
              "interval": "day"
            }
          }
        }
      }
    },
    "format": {"property": "aggregations.order_over_time.buckets"}
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "key", "type": "temporal", "axis": {"title": false}},
    "y": {
      "field": "doc_count",
      "type": "quantitative",
      "axis": {"title": "Call count"}
    }
  }
}

```

Then adjust the `query` to match the documents you need.

---

<div class="post-metadata">

**Author:** ![nb03briceno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nb03briceno/32/45777_2.png) [@nb03briceno](https://discuss.elastic.co/u/nb03briceno)\
**Post date:** [April 17, 2020, 12:08pm UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/4 "2020-04-17T12:08:16Z")

</div>

Hello, Lucca, I'm willing to do the work with vega because we have been developping other graphs with this graph tool in kibana. Is there a way for me to send you the last code correctation within vega to see why the plot is not having any result?. I tried the aggregations in the dev tools and it is working properly, I made the corrections you suggested but I'm not having different results.

"key" , "doc\_count" must be the fields selected by default when I do the plot with the aggregation?, cause those fields I dont have in my index (hs\_index), and for example I saw sometimes people refers to the Y fields suchs as the "name of the aggregation".value  
? Would you mind explainning me this ? last time we developed a graph was in a total different way.

Thanks so much!

Thanks for your answer.

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 17, 2020, 1:48pm UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/5 "2020-04-17T13:48:47Z")

</div>

The `doc_count` and `key` are fields generated by the aggregation you've specified.  
I just discovered an error (because I tested it on a different time field: mine was `timestamp` while yours is `date_time`). Try this:

```auto
{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "title": "Daily Count",
  "data": {
    "url": {
      "index": "hs_index",
      "body": {
        "size": 0,
        "track_total_hits": true,
        "query": {"match_all": {}},
        "aggregations": {
          "order_over_time": {
            "date_histogram": {
              "field": "date_time",
              "format": "yyyy-MM-dd hh:mm-ss",
              "interval": "day"
            }
          }
        }
      }
    },
    "format": {"property": "aggregations.order_over_time.buckets"}
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "key", "type": "temporal", "axis": {"title": false}},
    "y": {
      "field": "doc_count",
      "type": "quantitative",
      "axis": {"title": "Call count"}
    }
  }
}

```

I strongly suggest using native visualizations.  
It is straightforward (see [here](https://www.elastic.co/guide/en/kibana/current/visualize.html)).

---

<div class="post-metadata">

**Author:** ![nb03briceno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nb03briceno/32/45777_2.png) [@nb03briceno](https://discuss.elastic.co/u/nb03briceno)\
**Post date:** [April 17, 2020, 3:53pm UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/6 "2020-04-17T15:53:38Z")

</div>

Thanks so much, now it is working properly! :). I would like just to ask you one last question, do you know if it is possible to change the way in which the scale of an axis is depicted in Vega? (supposing the X axis in my example) when I change the aggregation to minutes or seconds, the whole amount labeled values of the axis are grouped too close so its not possible to see them or differentiate them.it is possible to keep some space between each point and have a longer X- axis that i could move in with an scroll bar or something similar?

Thanks so much

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 17, 2020, 5:30pm UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/7 "2020-04-17T17:30:52Z")

</div>

I am not an expert on Vega unfortunately...  
I would suggest checking the official documentation at [https://vega.github.io/vega/docs/](https://vega.github.io/vega/docs/) and grab hints from the examples

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 15, 2020, 5:31pm UTC](https://discuss.elastic.co/t/problem-date-histogram-in-vega-with-aggregation/228487/8 "2020-05-15T17:31:08Z")

</div>

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