gul.cabuk
gul.cabuk

Reputation: 1

Elasticsearch not allowing brackets for fields name for scripting

I've tried to use update api. I've inserted a document which contains a list.

INSERT:

curl -XPOST "http://localhost:9200/t/t/1" -d' 
{ 
"hobbies(first)" : ["a", "b"] 
}' 

UPDATE QUERY:

curl -XPOST localhost:9200/t/t/1/_update?pretty -d '{   "script" : {
       "inline": "ctx._source.hobbies(first).add(params.new_hobby)",
       "params" : {
          "new_hobby" : "c"
       }
   }
}'

ERROR:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[aaBiwwv][172.17.0.2:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "Variable [first] is not defined."
      },
      "script_stack" : [
        "ctx._source.hobbies(first).add(params.new_hob ...",
        "                    ^---- HERE"
      ],
      "script" : "ctx._source.hobbies(first).add(params.new_hobby)",
      "lang" : "painless"
    }
  },
  "status" : 400
}

When I've tried to update list, I've got error above. I've realized that when I removed part with brackets('(first)') from my field's name, it's working. How can I prepare an update query with a field name with brackets?

Thanks in advance.

Upvotes: 0

Views: 1320

Answers (1)

alr
alr

Reputation: 1804

this is a horrible convention for field names, just stick with alphanumerics (please keep in mind, that someone after you has to maintain this, so it would be so much nicer to work with cleaner data in Elasticsearch). You can try ctx.source['hobbies(first)']

Upvotes: 1

Related Questions