# How to clear cache of keyword field in elastic search?

**URL:** <https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026>\
**Category:** Elasticsearch\
**Tags:** aggregations\
**Created:** [September 4, 2024, 10:24am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026 "2024-09-04T10:24:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhavyagc](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@Bhavyagc](https://discuss.elastic.co/u/Bhavyagc)\
**Post date:** [September 4, 2024, 10:24am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/1 "2024-09-04T10:24:27Z")

</div>

Hi ,

I am facing cache issue with keyword field in Elastic. I have a document with keyword field which is indexed with some value example : **abc** later the same document is reindexed again with some value **abcd** , old value still present in aggregation. How to remove the old value **abc** with count as zero from aggregation.

---

<div class="post-metadata">

**Author:** ![Bhavyagc](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@Bhavyagc](https://discuss.elastic.co/u/Bhavyagc)\
**Post date:** [September 24, 2024, 1:51am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/2 "2024-09-24T01:51:59Z")

</div>

Can someone help me how to clear aggregation cache?

I have used clear cache API on fields and on index as well which seems not working

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [September 24, 2024, 2:56am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/3 "2024-09-24T02:56:00Z")

</div>

Hi @Bhavyagc

You will need to show an example as that does not sound like correct behavior...

Perhaps the document is not really being updated, but adding another document.

Can you show some repeatable code / example?

---

<div class="post-metadata">

**Author:** ![Bhavyagc](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@Bhavyagc](https://discuss.elastic.co/u/Bhavyagc)\
**Post date:** [October 30, 2024, 5:51am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/4 "2024-10-30T05:51:16Z")

</div>

Hi @stephenb ,

Please find the below example and the exact issue I am facing....

I'm having some problems with aggregations returning old data. When I query directly using `bool/must/term` the data is not there - as it should. Only in aggregation results.

1. I have a property called `foo`.
2. Then I updated all the documents where `foo` had the value from `x` to `y`.

Querying directly for `foo` = `x` yields 0 results (Old value) and `foo` = `y` yields 10 results (New value)

When aggregating `foo' I receive `x` in the `foo` property (with`doc\_count`=`0`) and `y` in the `foo` property (with`doc\_count`=`10`, which seems correct) . why still **x** is appearing in **foo** property with zero doc count?

Elasticsearch version `8.15` running in Elastic Cloud.

Note: when I removed the index completely and populated the index again, everything was working as it should. It only appears to be happening on existing indices.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 30, 2024, 2:46pm UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/5 "2024-10-30T14:46:54Z")

</div>

Hi @Bhavyagc

When I asked for a sample code this is something like I meant..

This code does not produce the result you are seeing so you will need to show us a simple example that shows your results.

Not to say what you are seeing is not valid you just need to show use what you are actually doing...

```auto
PUT myindex
{
  "mappings": {
    "properties": {
      "foo" : {"type": "keyword"}
    }
  }
}
  
PUT myindex/_doc/1
{
  "foo" : "x"
}

PUT myindex/_doc/2
{
  "foo" : "x"
}

PUT myindex/_doc/3
{
  "foo" : "x"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo"
      }
    }
  }
}

PUT myindex/_doc/1
{
  "foo" : "y"
}

PUT myindex/_doc/2
{
  "foo" : "y"
}

PUT myindex/_doc/3
{
  "foo" : "x"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo"
      }
    }
  }
}

PUT myindex/_doc/1
{
  "foo" : "y"
}

PUT myindex/_doc/2
{
  "foo" : "y"
}

PUT myindex/_doc/3
{
  "foo" : "y"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo"
      }
    }
  }
}

```

The results of the aggregation in order are as expected

```auto
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "x",
          "doc_count": 3
        }
      ]
    }
  }
}

  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 2
        },
        {
          "key": "x",
          "doc_count": 1
        }
      ]
    }
  }
}

  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 3
        }
      ]
    }
  }
}

```

BTW I also tried with the `_update` API  
example

```auto
POST myindex/_update/1
{
  "script" : "ctx._source.foo = 'y'"
}

```

Can you try a `_refresh` after you have finished your updates?  
You can look at [Shard request cache settings](https://www.elastic.co/guide/en/elasticsearch/reference/8.15/shard-request-cache.html) page as well for addition setting

---

<div class="post-metadata">

**Author:** ![Bhavyagc](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@Bhavyagc](https://discuss.elastic.co/u/Bhavyagc)\
**Post date:** [November 4, 2024, 12:52pm UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/6 "2024-11-04T12:52:21Z")

</div>

Thank you for your response.

Can you please try with **min\_doc\_count** in the query post updating all values from `x` to `y` ?

```auto
GET myindex/_search
{
  "size": 0, 

  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

```

Below is the results of aggregation

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 3
        },
        {
          "key": "x",
          "doc_count": 0
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [November 4, 2024, 4:01pm UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/7 "2024-11-04T16:01:34Z")

</div>

Not sure what you are doing

```auto
PUT myindex
{
  "mappings": {
    "properties": {
      "foo" : {"type": "keyword"}
    }
  }
}
  
PUT myindex/_doc/1
{
  "foo" : "x"
}

PUT myindex/_doc/2
{
  "foo" : "x"
}

PUT myindex/_doc/3
{
  "foo" : "x"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

POST myindex/_update/1
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/2
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/3
{
  "script" : "ctx._source.foo = 'x'"
}

GET _cat/indices/myindex?v

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

POST myindex/_update/1
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/2
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/3
{
  "script" : "ctx._source.foo = 'y'"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

```

Last search results in

```auto

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 3
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [November 4, 2024, 6:38pm UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/8 "2024-11-04T18:38:42Z")

</div>

Can you provide a repeatable example?

---

<div class="post-metadata">

**Author:** ![Bhavyagc](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@Bhavyagc](https://discuss.elastic.co/u/Bhavyagc)\
**Post date:** [November 5, 2024, 7:22am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/9 "2024-11-05T07:22:46Z")

</div>

Please find the steps I followed and the query.

```auto
PUT myindex
{
  "mappings":{
    "properties": {
      "foo" : {"type": "keyword"}
    }
  }
}
  
PUT myindex/_doc/1
{
  "foo" : "x"
}

PUT myindex/_doc/2
{
  "foo" : "x"
}

PUT myindex/_doc/3
{
  "foo" : "x"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}
  
POST myindex/_update/1
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/2
{
  "script" : "ctx._source.foo = 'y'"
}

POST myindex/_update/3
{
  "script" : "ctx._source.foo = 'x'"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

POST myindex/_update/3
{
  "script" : "ctx._source.foo = 'y'"
}

GET myindex/_search
{
  "size": 0, 
  "aggs": {
    "foo_agg": {
      "terms": {
        "field": "foo",
        "min_doc_count": 0
      }
    }
  }
}

```

Please find the below out put in the same order of search agg query...

output 1:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "x",
          "doc_count": 3
        }
      ]
    }
  }

```

Output 2:

```auto
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 2
        },
        {
          "key": "x",
          "doc_count": 1
        }
      ]
    }
  }
}

```

Output 3:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "foo_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "y",
          "doc_count": 3
        },
        {
          **"key": "x",**
          **"doc_count": 0**
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 5, 2024, 9:43am UTC](https://discuss.elastic.co/t/how-to-clear-cache-of-keyword-field-in-elastic-search/366026/10 "2024-11-05T09:43:41Z")

</div>

Why are you setting min\_doc\_count?  
If you don't want the value of 0 to come back, you probably need to remove this or set it to 1.

Otherwise, may be a forcemerge will clean this. Could you try?
