Suresh AK
Suresh AK

Reputation: 115

Update elastic search data with new key-value pair

{
   "took": 0,
   "timed_out": false,
   "_shards": {
   "total": 5,
   "successful": 5,
   "skipped": 0,
   "failed": 0
},
"hits": {
"total": 25,
"max_score": 1,
"hits": [
  {
    "_index": "surtest1",
    "_type": "catalog",
    "_id": "prod_9876561740",
    "_score": 1,
    "_source": {
      "id": "01",
      "type": "product" 
    }
  },
  {
    "_index": "surtest1",
    "_type": "catalog",
    "_id": "prod_9876543375",
    "_score": 1,
    "_source": {
      "id": "02",
      "type": "product" 
    }
  }
]
}
}

This is the sample json response of search inside elastic search. We need to add one more key-value pair("spec":"4gb") in all the json object like,

"_source": {
      "id": "01",
      "type": "product" ,
      "spec": "4gb"
    },
"_source": {
      "id": "02",
      "type": "product" ,
      "spec": "4gb"
    }

this updation should be in a single command.Please guide us to perform this operation.

Upvotes: 3

Views: 2702

Answers (2)

sramalingam24
sramalingam24

Reputation: 1337

Try

POST /surtest1/_update_by_query?refresh
{
"script": {
    "source": "ctx._source['spec']='4gb'"
 }
}

Upvotes: 2

Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

Take a look at Update By Query API. You are able to prepare a query to match all documents and use scripting to add the property you want.

Example:

POST twitter/_update_by_query
{
  "script": {
    "source": "ctx._source.likes++",
    "lang": "painless"
  },
  "query": {
    "term": {
      "user": "kimchy"
    }
  }
}

Upvotes: 0

Related Questions