# Delete By query On Fields of type Text

**URL:** <https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427>\
**Category:** Elasticsearch\
**Created:** [May 15, 2023, 8:43am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427 "2023-05-15T08:43:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martim\_Mourao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martim_mourao/32/118963_2.png) [@Martim\_Mourao](https://discuss.elastic.co/u/Martim_Mourao)\
**Post date:** [May 15, 2023, 8:43am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/1 "2023-05-15T08:43:20Z")

</div>

Elasticsearch Version: 8.7.1

We needed to do some deletes by Query using: [Delete by query API | Elasticsearch Guide [8.7] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html#docs-delete-by-query)

Our Request using dev tools on Kibana:

```auto
POST /INDEX/_delete_by_query
{
  "query": {
      "match": {
      "context.label": "TEST 111111-1"
    }
  }
}

```

This query deleted all documents with the substring: "TEST". And not only the exact match: "TEST 111111-1".  
We used this request before and only deleted exact matches!  
Can you help us understand our error or if this is a major bug on this version?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 15, 2023, 9:06am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/2 "2023-05-15T09:06:59Z")

</div>

My guess is that the field you are running the query against is mapped as `text`, which means that the text is tokenised. If you ran it against a field mapped as `keyword` it would have behaved as you described.

If the mappings are as described what you are seeing is expected. If you are going to run a one-off DBQ I would recommend to always run the query separately and verify that it matches what you expect.

---

<div class="post-metadata">

**Author:** ![Martim\_Mourao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martim_mourao/32/118963_2.png) [@Martim\_Mourao](https://discuss.elastic.co/u/Martim_Mourao)\
**Post date:** [May 15, 2023, 9:39am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/3 "2023-05-15T09:39:20Z")

</div>

I can confirm that type of the field is text.

How can I update a field of the type text only on exact matches?

Thanks you for the clarification!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 15, 2023, 9:40am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/4 "2023-05-15T09:40:49Z")

</div>

You can generally not update mappings without reindexing. If you are using dynamic mappings there may be a subfield named `context.label.keyword` mapped as `keyword` that you can use.

---

<div class="post-metadata">

**Author:** ![Martim\_Mourao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martim_mourao/32/118963_2.png) [@Martim\_Mourao](https://discuss.elastic.co/u/Martim_Mourao)\
**Post date:** [May 15, 2023, 9:47am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/5 "2023-05-15T09:47:55Z")

</div>

For this field, I do not have a keyword option.

So if I understood correctly I would need to reindex this index to add a keyword field?

Notes:  
If I try to update or delete docs filtering using queries I should always use the fields with ".keyword" If I do not use this it would update or delete using the substrings of the filter.

---

<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:** [June 12, 2023, 9:47am UTC](https://discuss.elastic.co/t/delete-by-query-on-fields-of-type-text/333427/6 "2023-06-12T09:47:56Z")

</div>

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