Search code examples
elasticsearchelasticsearch-painless

In Elasticsearch how can I get field length using the painless script?


I'm trying to update a field if it's longer than the existing one in an index, but neither of these function seem to work to get the length of a field

ctx._source.description.length() < params.description.length()
ctx._source.description.size() < params.description.size()
ctx._source.description.length < params.description.length

I get the same error that the methods don't exist. Any idea how to achieve this?

Edit:

This is the error I'm getting:

array:1 [
  "update" => array:5 [
    "_index" => "products_a"
    "_type" => "_doc"
    "_id" => "XjouMXoBeY37PI1TSOQl"
    "status" => 400
    "error" => array:3 [
      "type" => "illegal_argument_exception"
      "reason" => "failed to execute script"
      "caused_by" => array:6 [
        "type" => "script_exception"
        "reason" => "runtime error"
        "script_stack" => array:2 [
          0 => """
            if (ctx._source.description.length()<params.description.length()) {\n
            \t\t\t\t\t\t\t\t
            """
          1 => "                           ^---- HERE"
        ]
        "script" => "add_new_seller_to_existing_doc"
        "lang" => "painless"
        "caused_by" => array:2 [
          "type" => "illegal_argument_exception"
          "reason" => "dynamic method [java.util.ArrayList, length/0] not found"
        ]
      ]
    ]
  ]
]

Edit 2:

Stored script

    $params = [
        'id' => 'add_new_seller_to_existing_doc',
        'body' => [
            'script' => [
                'lang' => 'painless',
                'source' =>
                    'if(params.containsKey(\'description\')){
                        if (!ctx._source.containsKey(\'description\')) {
                            ctx._source.description=params.description;
                        } else if (ctx._source.description.length()<params.description.length()) {
                            ctx._source.description=params.description;
                        }
                    }'
            ]
        ]
    ];

Bulk update command:

    $bulk_obj['body'][] = ['update' => ['_id' => $id, '_index' => 'products']];

    $bulk_obj['body'][] = ['id' => 'add_new_seller_to_existing_doc', 'params' => ['description'=>'some_description']];

Solution

  • The problem seems to be that the description field is an ArrayList awhile you think it is a String.

    Either you use ArrayList.size() or you convert the ArrayList to a String (if it contains only one string) and you can then use String.length()