cannot resolve symbol[string] when using updateByQuery with ElasticSearch

3.9k views Asked by At

I have the following set-up:

mapping:

esClient.indices.putMapping({
  index: 'tests',
  body: {
    properties: {
      name: {
        type: 'text',
      },
      lastName: {
        type: 'text',
      },
    },
  },
});

this is the result when I post an entry:

enter image description here

this is the result when I query the entries:

curl -X GET "localhost:9200/tests/_search?pretty" -H 'Content-Type: application/json' -d'       
{
"size": 1000,
"query" : {
    "match_all" : {}
  }
}
'
{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "tests",
        "_type" : "_doc",
        "_id" : "KJbtj3kBRlqnip7VJLLI",
        "_score" : 1.0,
        "_source" : {
          "lastName" : 1,
          "name" : "lucas"
        }
      }
    ]
  }
}

I tried to update the entry's last name with the following curl:

curl -X POST "localhost:9200/tests/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.lastName='johnson'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name": "lucas"
    }
  }
}
'

AND THIS IS THE ERROR I'M GETTIN:

    {
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "ctx._source.lastName=johnson",
          "                     ^---- HERE"
        ],
        "script" : "ctx._source.lastName=johnson",
        "lang" : "painless",
        "position" : {
          "offset" : 21,
          "start" : 0,
          "end" : 28
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "compile error",
    "script_stack" : [
      "ctx._source.lastName=johnson",
      "                     ^---- HERE"
    ],
    "script" : "ctx._source.lastName=johnson",
    "lang" : "painless",
    "position" : {
      "offset" : 21,
      "start" : 0,
      "end" : 28
    },
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "cannot resolve symbol [johnson]"
    }
  },
  "status" : 400
}

If I put an integer instead of a string it updates it otherwise I keep getting that error.

Thanks a lot for your help.

2

There are 2 answers

5
ESCoder On

You need to surround the new lastName field value with ' '.

Adding a working example

Index Data:

{
    "name":"lucas",
    "lastName":"erla"
}

Query:

POST _update_by_query
{
  "script": {
    "source": "ctx._source.lastName='johnson'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name": "lucas"
    }
  }
}

After hitting the update by query API, the document will be updated to

GET /_doc/1

{
  "_index": "67641538",
  "_type": "_doc",
  "_id": "1",
  "_version": 3,
  "_seq_no": 2,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "lastName": "johnson",
    "name": "lucas"
  }
}
0
sharif779 On

You need to surround the new lastName field value with " ".

I faced similar problem and I solved by adding " " instead of ' '. My ES version is 8.4.

QUERY.

 

    curl -X POST "localhost:9200/tests/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
    {
      "script": {
        "source": "ctx._source.lastName=\"johnson\"",
        "lang": "painless"
      },
      "query": {
        "term": {
          "name": "lucas"
        }
      }
    }
    '