ankita shukla
ankita shukla

Reputation: 47

Elastic Search: To get all the document id depending on _field in source and update the _field with new data

I wanted to fetch all the document ids present in elastic search repo depending on a field data and update that field with new data.

Ex: "_index": "conn2", "_type": "conn2", "_id": "doc1537857086536", "_score": 1, "_source": { "suitename": "TestSuite"

I want to replace suitename with "TestSuite2" where suitename="TestSuite" for all the document id which contains "suitename": "TestSuite"

Please help me if any existing API i can use or any approach.

Thanks

Upvotes: 1

Views: 113

Answers (1)

Tim
Tim

Reputation: 1286

You can use the Update by Query API.

Your call would look something like this:

[EDIT]: Updated this call to use a script to update the suitename field.

POST conn2/_update_by_query
{
  "script": {
    "inline": "ctx._source.suitename = 'TestSuite2'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "suitename": "TestSuite"
    }
  }
}

Alternatively, you could also use _reindex.

POST _reindex
{
  "source": {
    "index": "conn2"
  },
  "dest": {
    "index": "new_index"
  },
  "script": {
    "source": "if (ctx._source.suitename == 'TestSuite') {ctx._source.suitename = 'TestSuite2'}",
    "lang": "painless"
  }
}

Upvotes: 0

Related Questions