Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch script - variable not defined

I'm following an Elasticsearch tutorial, but ran into a problem when trying to use parameters in a script.

Step 1: Create a new document - OK (index = website; type = blog; id = 1)

curl -XPUT localhost:9200/website/blog/1?pretty -d '{
  "title":"my first post",
  "tags" : [ "tag1" ]
}'

Step 2: Use script to append an extra value to tags array - ERROR

curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
   "script" : "ctx._source.tags+=new_tag",
   "params" : {
      "new_tag" : "tag2"
   }
}'

The error message is this, mentioning "reason" : "Variable [new_tag] is not defined." But I have defined the variable new_tag as described on the tutorial page. What am I doing wrong?

  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[mrukUvA][127.0.0.1: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 [new_tag] is not defined."
      },
      "script_stack" : [
        "ctx._source.tags+=new_tag",
        "                  ^---- HERE"
      ],
      "script" : "ctx._source.tags+=new_tag",
      "lang" : "painless"
    }
  },
  "status" : 400
}

Step 2 (retry) Qualifying new_tag with params - ERROR

curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
   "script" : {
       "inline": "ctx._source.tags+=params.new_tag",
       "params" : {
          "new_tag" : "tag2"
       }
   }
}'

Gives error

{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[mrukUvA][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "runtime error",
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "Cannot cast java.lang.String to java.util.ArrayList"
      },
      "script_stack" : [
        "ctx._source.tags+=params.new_tag",
        "                        ^---- HERE"
      ],
      "script" : "ctx._source.tags+=params.new_tag",
      "lang" : "painless"
    }
  },
  "status" : 400
}

As a sanity check to make sure the document is valid

$ curl -XGET localhost:9200/website/blog/1?pretty
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 27,
  "found" : true,
  "_source" : {
    "title" : "my first post",
    "tags" : [
      "tag1"
    ]
  }
}

So the document does have the valid field tag which is an array.

like image 473
kgf3JfUtW Avatar asked Mar 28 '17 20:03

kgf3JfUtW


2 Answers

you can use "inline" although it is deprecated. now you alse can use "source" to replace "inline" for no warnings. for example:

"script" : {
     "source": "ctx._source.tags.add(params.new_tag)",
     "params": {
       "new_tag":"tag1"
     }
   }
like image 194
varuscn Avatar answered Nov 16 '22 04:11

varuscn


Your syntax is slightly off, if you have parameters you need to inline the script. Try this:

curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
   "script" : {
       "inline": "ctx._source.tags.add(params.new_tag)",
       "params" : {
          "new_tag" : "tag2"
       }
   }
}'
like image 21
Val Avatar answered Nov 16 '22 04:11

Val