0

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.

gul.cabuk
  • 1
  • 4
  • 1
    Please replace parentheses by an underscore. It is definitely not a good practice have document proprieties with it. "hobbies(first)" --> "hobbies_first" Good link for naming convention: https://www.elastic.co/guide/en/beats/libbeat/current/event-conventions.html – nozari Aug 27 '17 at 02:12
  • I'm generating my field names dynamically and there are fields like company(company_code) which means company field is prepared using company_code relation, so I'm trying to make more understandable my fields. Is there a way you know to use brackets in scripts? – gul.cabuk Aug 27 '17 at 12:06
  • I suggest you to create a function that would replace brackets to underscores instead of try to make this work. – nozari Aug 29 '17 at 20:57

1 Answers1

1

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)']

alr
  • 1,744
  • 1
  • 10
  • 11
  • I've tried using apostrophes and square brackets before. For example it's working: **curl -XPOST localhost:9200/t/t/1/_update?pretty -d '{"script":{"inline":"ctx._source.'hobbies'.add(params.new_hobby)","params":{"new_hobby":"c"}}}'** not working: **curl -XPOST localhost:9200/t/t/1/_update?pretty -d '{"script":{"inline":"ctx._source['hobbies'].add(params.new_hobby)","params":{"new_hobby":"c"}}}'**. But both of them not working with a field name with brackets. – gul.cabuk Aug 28 '17 at 09:12