# Node Query Cache with keyword

**URL:** <https://discuss.elastic.co/t/node-query-cache-with-keyword/285314>\
**Category:** Elasticsearch\
**Created:** [September 28, 2021, 6:59am UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314 "2021-09-28T06:59:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yukai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yukai/32/95224_2.png) [@yukai](https://discuss.elastic.co/u/yukai)\
**Post date:** [September 28, 2021, 6:59am UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314/1 "2021-09-28T06:59:44Z")

</div>

For search performance, changing field type of integer/long to keywords for term querys might increase search speed.

However, I recently found that doing this change would no longer getting benefit from node query cache in a filter context

Is there any solution can reuse node query cache?

Appreciating any answer

---

<div class="post-metadata">

**Author:** ![yukai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yukai/32/95224_2.png) [@yukai](https://discuss.elastic.co/u/yukai)\
**Post date:** [September 28, 2021, 7:03am UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314/2 "2021-09-28T07:03:56Z")

</div>

Query DSL would be like as below:

```auto
GET myindex/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "myfield": "666"
          }
        }
      ]
    }
  }
}

```

when "myfield" is a type of "integer" , the query\_cache would be increased from calling:  
`GET myindex/_stats`

But when I change the type of integer to a keyword, the cache no longer working

---

<div class="post-metadata">

**Author:** ![yukai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yukai/32/95224_2.png) [@yukai](https://discuss.elastic.co/u/yukai)\
**Post date:** [September 28, 2021, 7:57am UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314/3 "2021-09-28T07:57:26Z")

</div>

solved by myself

```auto
GET myindex/_search
{
  
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "field1": "666"
                }
              }
            ],
            "should": [
              {
                "term": {
                  "field1": "666"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

look like ugly but it did help

---

<div class="post-metadata">

**Author:** ![yukai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yukai/32/95224_2.png) [@yukai](https://discuss.elastic.co/u/yukai)\
**Post date:** [September 28, 2021, 12:18pm UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314/4 "2021-09-28T12:18:23Z")

</div>

The magic is . Within a filter context:

Term query with numeric type will be solved as PointRangeQuery in Lucence(covered in node query cache)

while, Term query with keyword type will be plainly solved as TermQuery in Lucnce(not cached)

The solution is, Instead of using term query directly, you can use a boolean query with multiply conditions(term queries each), So Elasticsearch treats boolean query as BooleanQuery in lucence in a whole, and using node query cache again(BooleanQuery would be cached)

---

<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:** [October 26, 2021, 12:18pm UTC](https://discuss.elastic.co/t/node-query-cache-with-keyword/285314/5 "2021-10-26T12:18:56Z")

</div>

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