1

i have a bank accounts dataset which have the fields account number,state,firstname,lastname.iam writing the following command to update all the firstname records of the customers from state CA to antony.

curl -XPOST 'localhost:9200/bank/_update_by_query?pretty' -d '{
 "query" : {
  "term" : {"state":"CA"}
 },
 "script" : {
  "inline" : "ctx._source.firstname = a",
  "params" : {
   "a" : "antony"
  },
  "lang" : "groovy"
 }
}'

result:

{ "took" : 3, "timed_out" : false, "total" : 0, "updated" : 0, "batches" : 0, "version_conflicts" : 0, "noops" : 0, "retries" : 0, "failures" : [ ] }

None of the records are getting updated.

thanks in advance.

sandy kay
  • 115
  • 1
  • 14
  • Which version of ES are you running? And are you sure you have [enabled dynamic scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#enable-dynamic-scripting)? – Val Jul 27 '16 at 13:20
  • version 2.3.4. and yes, i enabled dynamic scripting – sandy kay Jul 27 '16 at 13:31
  • is the `state` field `analyzed` or `not_analyzed`? i.e. how many documents do you get if you run the query separately? – Val Jul 27 '16 at 13:32
  • i did not mention anything while indexing and there are 8 documents with state CA – sandy kay Jul 27 '16 at 13:45

3 Answers3

1

Since your state field is analyzed (by default), you need to use a match query instead of a term one (or keep the term query but lowercase CA to ca)

curl -XPOST 'localhost:9200/bank/_update_by_query?pretty' -d '{
 "query" : {
  "match" : {"state":"CA"}
 },
 "script" : {
  "inline" : "ctx._source.firstname = a",
  "params" : {
   "a" : "antony"
  },
  "lang" : "groovy"
 }
}'
Val
  • 207,596
  • 13
  • 358
  • 360
0

If somebody wants to do this just simply through Kibana just use this script as the sample to add in Dev Tools redactor in Kibana, here you are:

POST persons/_update_by_query?conflicts=proceed
{
  "query": {
    "term": {"person_id": 249917}
  },
  "script": {
    "inline": "ctx._source.dri_for_buyers = 4"
  }
}
Alexander Tunick
  • 517
  • 1
  • 6
  • 20
-2

If you want to update the mapping for your filed state then u first need to delete the mapping and recreate again in 2.x because while updating it will not update your mapping properly in Es 2.2.

As per Elatic search document : We can update a mapping to add a new field, but we can’t change an existing field from analyzed to not_analyzed.

First you can take the backup of your mapping. then delete it using below link. curl -XDELETE 'http://host.sachin.com:9999/ecmviews/?pretty'

after deleting the mapping, u can you the same backup mapping and update the state filed like below: "state" : { "type" : "string", "index": "not_analyzed" },

and update mapping json using below command: curl -XPUT 'http://host.sachin.com:9999/ecmviews' -d '{ your mapping json without curl backets } . it will update the mapping with ack:true as response. after updating the mapping you can try your query. it will work.

sachin
  • 11
  • 2
  • This appears to be self-promotion, which is considered spam when not done within the guidelines. Please see: [How to not be a spammer](http://stackoverflow.com/help/promotion). – Makyen Apr 14 '17 at 06:21
  • Please add code and errors as text [using code formatting](http://stackoverflow.com/editing-help#code). – Makyen Apr 14 '17 at 06:26