在 Elasticsearch 中,如何使用无痛脚本获取字段长度?

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

我正在尝试更新一个字段,如果它比索引中的现有字段长,但这些函数似乎都无法获取字段的长度

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

我得到同样的错误,方法不存在。知道如何实现吗?

编辑:

这是我遇到的错误:

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"
        ]
      ]
    ]
  ]
]

编辑 2:

存储脚本

    $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_obj['body'][] = ['update' => ['_id' => $id, '_index' => 'products']];

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

问题似乎是 description 字段是一个 ArrayList,而您认为它是一个字符串。

您可以使用 ArrayList.size() 或将 ArrayList 转换为字符串(如果它只包含一个字符串),然后您可以使用 String.length()