简体   繁体   中英

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"

  • I tried using Update API where as it is required to mention document id.

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

Thanks

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"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM