Skip to content

Instantly share code, notes, and snippets.

@alexsisu
Created April 16, 2012 12:04
Show Gist options
  • Save alexsisu/2398248 to your computer and use it in GitHub Desktop.
Save alexsisu/2398248 to your computer and use it in GitHub Desktop.
ElasticSearch partial field update: remove element from array, does not work
curl -XPUT 'http://localhost:9200/testindex' -d '{
"settings":
{
"number_of_shards": 1,
"number_of_replicas": 0
}
}
'
curl -XPUT 'http://localhost:9200/testindex/posting/_mapping' -d '
{ "posting": {
"properties":
{
"id": {"type": "long", "store": "yes"},
"labels": {"type": "integer", "index_name": "label", "store": "yes"}
}
}
}
'
curl -XPOST 'http://localhost:9200/_bulk?pretty=true' -d '
{"index" : { "_index" : "testindex", "_type" : "posting", "_id" : "1" }}
{"labels":[], "id":1}
'
curl -XPOST 'http://localhost:9200/testindex/posting/1/_update' -d '{
"script" : "ctx._source.labels += label",
"params" : {
"label" : 5
}
}'
curl -XGET 'http://localhost:9200/testindex/posting/1'
curl -XPOST 'http://localhost:9200/testindex/posting/1/_update' -d '{
"script" : "ctx._source.labels -= label",
"params" : {
"label" : 5
}
}'
#result is {"error":"ElasticSearchIllegalArgumentException[failed to execute script]; nested: StringIndexOutOfBoundsException[String index out of range: -1]; ","status":400}
@arganzheng
Copy link

try this, it should work.

# Partial update with a script and params
POST /website/blog/1/_update
{
    "script" : "ctx._source.labels.remove(old_label)",
    "params" : {
       "old_label" : "search"
    }
}

remember to enable the dynamic script first by put the follow line in your elasticsearch.yml:

script.disable_dynamic: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment