0

How can I find and replace partial data in elasticsearch?

I have tried update_by_query. However, it replaces the whole value in a certain field which is not what I am looking for. What I am trying to achieve is to find 'cat' in a sentence and replace it with 'dog'

"this is a cat" ===> "this is a dog"

I have tried the following query:

        //     body: {
        //         script: {source: ctx._source.aa = params.term,
        //                 params: {term: dog},
        //                 lang: "painless"
        //         },
        //         query: {term: cat}
        // }};
Gelineau
  • 2,031
  • 4
  • 20
  • 30
  • Duplicated here with answer - https://stackoverflow.com/questions/47698187/how-to-replace-string-without-regexp-inside-painless-inline-script-for-aws-elast – Janani Sampath Kumar Oct 08 '21 at 13:59

1 Answers1

0

You can use replace method in script to do this

POST {index_name}/{type}/{id}/_update
{
  "script" : { 
    "source": "ctx._source.aa=ctx._source.aa.replace(\"cat\", params.term)",
    "params": { "term": "dog" },
    "lang": "painless"
  }
}

NOTE: The script syntax uses Elasticsearch 6.x.x

deerawan
  • 8,002
  • 5
  • 42
  • 51