# Kibana vega context and timefield

**URL:** <https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [January 21, 2022, 11:34pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102 "2022-01-21T23:34:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tingda\_Du](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tingda_du/32/96433_2.png) [@Tingda\_Du](https://discuss.elastic.co/u/Tingda_Du)\
**Post date:** [January 21, 2022, 11:34pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/1 "2022-01-21T23:34:17Z")

</div>

When I include the lines of

```auto
// Apply dashboard context filters when set
%context%: true
// Filter the time picker (upper right corner) with this field
%timefield%: @timestamp

```

I got this error:

```auto
* Infinite extent for field "by_currency_code_sum.value": [Infinity, -Infinity]

* Log scale domain includes zero: [,]

* Log scale domain includes zero: [,]

```

If I delete it, it works  
If I cannot do this way how to sync the vega plot with dashboard filters (dates, filters, etc.) _ **for general plots** _

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 22, 2022, 4:44am UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/2 "2022-01-22T04:44:00Z")

</div>

If no documents match the filter, such Infinity results could be generated. Please check the query with the filter and time stamp filters by itself. Is that result what you expected?

---

<div class="post-metadata">

**Author:** ![Tingda\_Du](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tingda_du/32/96433_2.png) [@Tingda\_Du](https://discuss.elastic.co/u/Tingda_Du)\
**Post date:** [January 22, 2022, 5:08am UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/3 "2022-01-22T05:08:34Z")

</div>

I only need the dashboard filters like dates can be applied to my vega plot, adding these two lines does not work. Also, for some plots with query block inside body it will give me

```auto
url.%context% and url.%timefield% must not be used when url.body.query is set

```

As I said, if this way does not work, any other way to resolve this is also acceptable.

I don't want to write deterministic query inside I want it to be interactive with the dashboard filter features outside. My original plot works fine the query return things properly.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 22, 2022, 6:38am UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/4 "2022-01-22T06:38:12Z")

</div>

> [@Tingda\_Du](#):
>
> I only need the dashboard filters like dates can be applied to my vega plot, adding these two lines does not work.

As you said your original plot works fine, the query without filtering must be correct. And the fact that the result change (to Infinity with your example) shows **the filtering works in some way**. As I said, there is a possibility that no document match the filter, so I recommended to check the query with filter outside vega.

> [@Tingda\_Du](#):
>
> Also, for some plots with query block inside body it will give me
> 
> ```auto
> url.%context% and url.%timefield% must not be used when url.body.query is set
> 
> ```

Yes, context and timefield filtering can be only used with aggregation clause and not with query clause.

---

<div class="post-metadata">

**Author:** ![Tingda\_Du](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tingda_du/32/96433_2.png) [@Tingda\_Du](https://discuss.elastic.co/u/Tingda_Du)\
**Post date:** [January 22, 2022, 3:32pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/5 "2022-01-22T15:32:26Z")

</div>

> Yes, context and timefield filtering can be only used with aggregation clause and not with query clause.

What will be the way to filter by dashboard time in this case? Is there a general way to do this for general plot?

> [@Tomo\_M](#):
>
> there is a possibility that no document match the filter, so I recommended to check the query with filter outside vega.

This possibility is 0, since I have checked that even if I changed to last two years it still gives me that error, which is impossible.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 22, 2022, 11:24pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/6 "2022-01-22T23:24:43Z")

</div>

> [@Tingda\_Du](#):
>
> What will be the way to filter by dashboard time in this case?

I suppose the only way to use context and timefield filtering is to implement the query using only aggregation.

If you used query clause to filter documents, you can use [Filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html) instead of query clause.

If you use "hits" of the query for formatter in vega-lite, it seems difficult but using filter aggregation with [scripted metric aggregation "trick"](https://discuss.elastic.co/t/how-to-aggregate-data-on-elastic-sent-by-logstash/205140/2) to retrieve all documents in a bucket could be a possible choice though I have not tried.

> [@Tingda\_Du](#):
>
> This possibility is 0

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/5/f577c3eed985bed9a4d428c397593f07181bb4ca.png)

I see. Please check the Request and Response in Inspector. Are there buckets as you expected?

---

<div class="post-metadata">

**Author:** ![Tingda\_Du](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tingda_du/32/96433_2.png) [@Tingda\_Du](https://discuss.elastic.co/u/Tingda_Du)\
**Post date:** [January 23, 2022, 3:55pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/7 "2022-01-23T15:55:41Z")

</div>

> [@Tomo\_M](#):
>
> I suppose the only way to use context and timefield filtering is to implement the query using only aggregation.

Do you think any of the code under [this section](https://www.elastic.co/guide/en/kibana/7.10/vega-reference.html#vega-queries) would be helpful other than the methods you mentioned?

> [@Tomo\_M](#):
>
> I see. Please check the Request and Response in Inspector. Are there buckets as you expected?

Thank you for this point, I will investigate this.

> [@Tomo\_M](#):
>
> If you use "hits" of the query for formatter in vega-lite

What do you mean by using "hits" of query for formatter? I don't understand this part you said.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 23, 2022, 4:21pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/8 "2022-01-23T16:21:47Z")

</div>

> [@Tingda\_Du](#):
>
> Do you think any of the code under [this section](https://www.elastic.co/guide/en/kibana/7.10/vega-reference.html#vega-queries) would be helpful other than the methods you mentioned?

Thanks! I've missed it. Sorry for wrongly informed you.

It could be a good way to make query using `"%dashboard_context-must_clause%"` or `"%dashboard_context-filter_clause%"`.. etc, without `%context%: true`.

---

<div class="post-metadata">

**Author:** ![Tingda\_Du](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tingda_du/32/96433_2.png) [@Tingda\_Du](https://discuss.elastic.co/u/Tingda_Du)\
**Post date:** [January 23, 2022, 5:09pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/9 "2022-01-23T17:09:05Z")

</div>

> [@Tomo\_M](#):
>
> It could be a good way to make query using `"%dashboard_context-must_clause%"` or `"%dashboard_context-filter_clause%"` .. etc, without `%context%: true` .

So I can do this for any es query? I can follow this template?

---

<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:** [February 20, 2022, 5:09pm UTC](https://discuss.elastic.co/t/kibana-vega-context-and-timefield/295102/10 "2022-02-20T17:09:23Z")

</div>

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