# \_update\_by\_query issue

**URL:** <https://discuss.elastic.co/t/update-by-query-issue/370226>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [November 8, 2024, 2:04pm UTC](https://discuss.elastic.co/t/update-by-query-issue/370226 "2024-11-08T14:04:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Megha30](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/megha30/32/139059_2.png) [@Megha30](https://discuss.elastic.co/u/Megha30)\
**Post date:** [November 8, 2024, 2:04pm UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/1 "2024-11-08T14:04:11Z")

</div>

Hi all,  
I am new to elasticsearh and here is my question.  
I appreciate your help 🙂

I have an index with the name my\_index. It has a field called as the Reason. I need to map each reason to a particular category. For eg:

Reason - A,C,E  
Category - car

Reason- B,D,F  
Category - bike

This is the way I have done it:

```auto
POST /my_index/_update_by_query
{
"script": {
    "source": """
      if (ctx._source.reason == 'A') { 
        ctx._source.Category = 'car'; 
      } else if (ctx._source.reason == 'transfer') { 
        ctx._source.Category = 'bike'; 
      } else if (ctx._source.reason == 'C') { 
        ctx._source.Category = 'car'; 
}
.
.
else {
        ctx._source.Category = 'Unknown' ;
      }
    """,
    "lang": "painless"
  },
"query": {
  "terms": {
    "reason": ["A","B","C","D","E","F"]
 }
  },
   "size": 1000
}

```

It created a new field and all the Categories are assigned based on the reason.

But the problem is, after sometime, some of the the values inside the categories show '-', even with 'reason', and they eventually show more and more records with '-'. And if I run the query again, theres no more '-'. and the cycle repeats.

what could be the issue and how to address this? Thanks for the help!

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [November 11, 2024, 7:27am UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/2 "2024-11-11T07:27:44Z")

</div>

Hi Megha, Are you looking to generate `Category` field on runtime while performing search or you want to create on data ingestion?

---

<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:** [November 11, 2024, 7:34am UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/3 "2024-11-11T07:34:39Z")

</div>

It sounds to me like you are indexing new data, for which the category is not set, or updating documents by overwriting with documents that do not have the categpry set. When you run update by query you only update the documents currently in the index and future inserts and updates are not affected. If you want the category to always be populated you could run your script in an ingext pipeline and have it apply to all cahnages, which could remove the need to run update-by-query.

---

<div class="post-metadata">

**Author:** ![Megha30](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/megha30/32/139059_2.png) [@Megha30](https://discuss.elastic.co/u/Megha30)\
**Post date:** [November 12, 2024, 3:25pm UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/4 "2024-11-12T15:25:01Z")

</div>

I believe data ingestion would be ideal

---

<div class="post-metadata">

**Author:** ![Megha30](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/megha30/32/139059_2.png) [@Megha30](https://discuss.elastic.co/u/Megha30)\
**Post date:** [November 12, 2024, 3:29pm UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/5 "2024-11-12T15:29:53Z")

</div>

Great! Thank you!  
I tried the way you suggested, I get run time errors.

```auto
PUT _ingest/pipeline/set_category_pipeline
{
"processors": [
{
"script": {
    "source": """
      if (ctx._source.reason == 'A') { 
        ctx._source.Category = 'car'; 
      } else if (ctx._source.reason == 'transfer') { 
        ctx._source.Category = 'bike'; 
      } else if (ctx._source.reason == 'C') { 
        ctx._source.Category = 'car'; 
}
.
.
else {
        ctx._source.Category = 'Unknown' ;
      }
    """
}
}

```

```auto
POST my_index/_doc/?pipeline=set_category_pipeline

{

"reason": "A"

}

{

"reason": "B"

}

{

"reason": "C"

}

```

Any help is appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![Megha30](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/megha30/32/139059_2.png) [@Megha30](https://discuss.elastic.co/u/Megha30)\
**Post date:** [November 13, 2024, 8:17am UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/6 "2024-11-13T08:17:43Z")

</div>

But can I generate the `Category` field on run time ? Does it update the field dynamically?

---

<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:** [December 11, 2024, 8:18am UTC](https://discuss.elastic.co/t/update-by-query-issue/370226/7 "2024-12-11T08:18:32Z")

</div>

Closed on December 11, 2024, 8:18am UTC

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