Skip to content

Instantly share code, notes, and snippets.

@dakrone
Created December 10, 2014 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dakrone/b19794f5f78aa055a5df to your computer and use it in GitHub Desktop.
Save dakrone/b19794f5f78aa055a5df to your computer and use it in GitHub Desktop.

Since String.format() is not in the whitelist, sometimes it's nice to be able to use string interpretation in scripts. Groovy allows doing this with GString interpretation.

Create an index

DELETE /script
{}

POST /script
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "doc": {
      "properties": {
        "body": {"type": "string"}
      }
    }
  }
}

{"acknowledged":true}
{"acknowledged":true}

Index docs

POST /script/doc/1?refresh
{"body": "foo"}

POST /script/doc/1/_update
{
  "script": "ctx._source.body = ctx._source.body + \"${bar}\"",
  "params": {
    "bar": " hi"
  }
}

GET /script/doc/1?_source=body
{}

{"_index":"script","_type":"doc","_id":"1","_version":1,"created":true}
{"_index":"script","_type":"doc","_id":"1","_version":2}
{"_index":"script","_type":"doc","_id":"1","_version":2,"found":true,"_source":{"body":"foo hi"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment