繁体   English   中英

在 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM