# Conditionally add a new field to existing data in elastic search

**URL:** <https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456>\
**Category:** Elasticsearch\
**Created:** [August 2, 2019, 6:24am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456 "2019-08-02T06:24:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![feb](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@feb](https://discuss.elastic.co/u/feb)\
**Post date:** [August 2, 2019, 6:24am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/1 "2019-08-02T06:24:08Z")

</div>

Hello there. New to Elastic Search. Kindly suggest a solution to add a new field conditionally to my document

current data in ElasticSearch

"\_index": "test",  
"\_type": "logs",  
"\_id": "AWDgIKFCvg3odC71dRbc",  
"\_score": 0.09685399,  
"\_source": {  
"HighTH": 120000,  
"@timestamp": "2018-01-10T12:52:02.684Z",  
"Cust": 16926,  
"areaCode": 123  
"application": "myapps",  
"@version": "1",  
"host": "localhost",  
"Run\_date": "2018-01-03T16:00:00.000Z",  
"LowTH": 10000,  
"message": "04-JAN-2018,MN,16926,206472,15785,120000,10000",  
"Trans": 206472,  
"Cntry\_code": "NO",  
"Trans\_GPS": 15785  
}

i need to add a field "gpsStrength": 209845 into data only if "Cust" = 16926 and "areaCode" = 123.

```
the required result need to be

"_index": "test",
"_type": "logs",
"_id": "AWDgIKFCvg3odC71dRbc",
"_score": 0.09685399,
"_source": {
  "HighTH": 120000,
  "@timestamp": "2018-01-10T12:52:02.684Z",
  "Cust": 16926,
  "areaCode": 123
  "application": "myapps",
  "@version": "1",
  "host": "localhost",
  "Run_date": "2018-01-03T16:00:00.000Z",
  "LowTH": 10000,
  "message": "04-JAN-2018,MN,16926,206472,15785,120000,10000",
  "Trans": 206472,
  "Cntry_code": "NO",
  "Trans_GPS": 15785,
  "gpsStrength": 209845
}

```

I tried using index type and id which is updating tha data

POST test/logs/AWDgIKFCvg3odC71dRbc/\_update?pretty { "doc": { "gpsStrength" : "209845" }

but required query based on \_source values

kindly note that i used dynamic mapping

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2019, 8:10am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/2 "2019-08-02T08:10:23Z")

</div>

check out the [update by query API](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/docs-update-by-query.html) allowing you to only update documents that match a query condition.

---

<div class="post-metadata">

**Author:** ![feb](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@feb](https://discuss.elastic.co/u/feb)\
**Post date:** [August 5, 2019, 4:56am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/3 "2019-08-05T04:56:02Z")

</div>

Thanks for the reply

I tried this. Working fine . Thanks

POST URL : test/\_update\_by\_query?conflicts=proceed

Query : { "script": { "inline": "ctx.\_source.gpsStrength= 209845", "lang": "painless" }, "query": { "bool": { "filter": [{ "term": { "Cust": 16926 }}, { "term": { "areaCode": "123"}}] } } }

Is there any way to do this update in bulk ? In my case "gpsStrength" field depends upon "Cust" and "areaCode"

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 5, 2019, 9:41am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/4 "2019-08-05T09:41:29Z")

</div>

you could extend your query to match more documents and then decide in the script which `gpsStrength` to set.

--Alex

---

<div class="post-metadata">

**Author:** ![feb](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@feb](https://discuss.elastic.co/u/feb)\
**Post date:** [August 6, 2019, 6:10am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/5 "2019-08-06T06:10:40Z")

</div>

Hi Alex,

I got your point, i tried and is right

But my current scenario is , i have more than million data. I can query with with only a unique field called "customerId". For each "customerId" i need to add a unique value field "customerPhoneNumber". If i use update by query i need to hit elastic search a million times.

Is [Batch Processing](https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-batch-processing.html#getting-started-batch-processing) a solution for this..?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 6, 2019, 8:37am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/6 "2019-08-06T08:37:22Z")

</div>

you can use the [bulk API](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-bulk.html) in combination with an update script. this requires you to know the ID of the document upfront. If that works for you, using the bulk API is the way to go

---

<div class="post-metadata">

**Author:** ![feb](https://avatars.discourse-cdn.com/v4/letter/f/df788c/32.png) [@feb](https://discuss.elastic.co/u/feb)\
**Post date:** [August 6, 2019, 10:05am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/7 "2019-08-06T10:05:03Z")

</div>

Thanks a lot..will work out with this

---

<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:** [September 3, 2019, 10:05am UTC](https://discuss.elastic.co/t/conditionally-add-a-new-field-to-existing-data-in-elastic-search/193456/8 "2019-09-03T10:05:14Z")

</div>

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