# Find a string and replace with another in all the documents

**URL:** <https://discuss.elastic.co/t/find-a-string-and-replace-with-another-in-all-the-documents/122624>\
**Category:** Elasticsearch\
**Created:** [March 6, 2018, 2:07am UTC](https://discuss.elastic.co/t/find-a-string-and-replace-with-another-in-all-the-documents/122624 "2018-03-06T02:07:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![udit.agg89](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@udit.agg89](https://discuss.elastic.co/u/udit.agg89)\
**Post date:** [March 6, 2018, 2:07am UTC](https://discuss.elastic.co/t/find-a-string-and-replace-with-another-in-all-the-documents/122624/1 "2018-03-06T02:07:13Z")

</div>

Hi,

The problem I am trying to solve is to update a field with a string with another string in all the documents wherever you find it.

So lets consider this:

1. I have 5 documents.
2. Two of the documents have string "text" in field abc and three of the documents have same string "text" in field xyz.
3. I want to update all the 5 documents replacing "text" with "text1".
4. I tried below query and it does update all the documents but in addition to that it adds a new field xyz in the documents which had field abc with string "text" and adds the field abc to the documents which had field xyz.

Query:  
POST log/\_update\_by\_query?refresh=true  
{  
"script": {  
"inline": "ctx.\_source.abc='text1';ctx.\_source.xyz='text1'"  
},  
"query": {  
"multi\_match" : {  
"query": "text",  
"fields": ["abc", "xyz"]  
}  
}  
}

---

<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:** [April 3, 2018, 2:07am UTC](https://discuss.elastic.co/t/find-a-string-and-replace-with-another-in-all-the-documents/122624/2 "2018-04-03T02:07:41Z")

</div>

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