簡體   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