# Filter context in Kibana generated search

**URL:** <https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210>\
**Category:** Kibana\
**Created:** [July 18, 2019, 1:46pm UTC](https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210 "2019-07-18T13:46:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![obuez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/obuez/32/71813_2.png) [@obuez](https://discuss.elastic.co/u/obuez)\
**Post date:** [July 18, 2019, 1:46pm UTC](https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210/1 "2019-07-18T13:46:33Z")

</div>

Hi all,

I'm taking a closer look at how Kibana actually build the search for visualizations and discoveries.  
What I can figure out is that every queries and Kibana filters are translated into a must clause in the generated json search. I would like Kibana to generate searches with filter context in a bool filter clause instead of a bool must clause. This way, I'll be able to benefit from the Node Query Cache (filter cache) hoping it will speed up a bit the load of my dashboards. Indeed, for my use-case, most of the visualizations filter on the same tag ☹ .

Even if I specify myself in the visualization the Kibana (6.8) filter as :

```json
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "tags.keyword": "my_tag"
        }
      }
    }
  }
}  

```

it will end up in something like :

```json
{
"query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        },
        {
          "match_all": {}
        },
        {
          "bool": {
            "filter": {
              "term": {
                "tags.keyword": "my_tag"
              }
            }
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": 1562852272157,
              "lte": 1563457072158,
              "format": "epoch_millis"
            }
          }
        },
        {
          "bool": {
            "filter": {
              "term": {
                "tags.keyword": "my_tag"
              }
            }
          }
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  }
}

```

which is not what I expect because the filter clause remains empty. I'm not sure if a nested filter in a must clause will give the same performance result.

How to force (somehow) Kibana to use a filter context to benefit from the Node Query Cache and avoid computing score or did I miss-understood something?

Cheers

---

<div class="post-metadata">

**Author:** ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)\
**Post date:** [July 25, 2019, 1:23pm UTC](https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210/2 "2019-07-25T13:23:27Z")

</div>

Hey @Bargs @lukas , can you help on this?

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [July 25, 2019, 8:56pm UTC](https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210/3 "2019-07-25T20:56:00Z")

</div>

There is a Github ticket for this issue [https://github.com/elastic/kibana/issues/8367](https://github.com/elastic/kibana/issues/8367)

---

<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:** [August 22, 2019, 8:56pm UTC](https://discuss.elastic.co/t/filter-context-in-kibana-generated-search/191210/4 "2019-08-22T20:56:00Z")

</div>

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