title: In Elasticsearch how can I get field length using the painless script?
body: <p>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</p>
  <pre><code>ctx._source.description.length() &lt; params.description.length()
  ctx._source.description.size() &lt; params.description.size()
  ctx._source.description.length &lt; params.description.length
  </code></pre>
  <p>I get the same error that the methods don't exist. Any idea how to achieve this?</p>
  <p>Edit:</p>
  <p>This is the error I'm getting:</p>
  <pre><code>array:1 [
    &quot;update&quot; =&gt; array:5 [
      &quot;_index&quot; =&gt; &quot;products_a&quot;
      &quot;_type&quot; =&gt; &quot;_doc&quot;
      &quot;_id&quot; =&gt; &quot;XjouMXoBeY37PI1TSOQl&quot;
      &quot;status&quot; =&gt; 400
      &quot;error&quot; =&gt; array:3 [
        &quot;type&quot; =&gt; &quot;illegal_argument_exception&quot;
        &quot;reason&quot; =&gt; &quot;failed to execute script&quot;
        &quot;caused_by&quot; =&gt; array:6 [
          &quot;type&quot; =&gt; &quot;script_exception&quot;
          &quot;reason&quot; =&gt; &quot;runtime error&quot;
          &quot;script_stack&quot; =&gt; array:2 [
            0 =&gt; &quot;&quot;&quot;
              if (ctx._source.description.length()&lt;params.description.length()) {\n
              \t\t\t\t\t\t\t\t
              &quot;&quot;&quot;
            1 =&gt; &quot;                           ^---- HERE&quot;
          ]
          &quot;script&quot; =&gt; &quot;add_new_seller_to_existing_doc&quot;
          &quot;lang&quot; =&gt; &quot;painless&quot;
          &quot;caused_by&quot; =&gt; array:2 [
            &quot;type&quot; =&gt; &quot;illegal_argument_exception&quot;
            &quot;reason&quot; =&gt; &quot;dynamic method [java.util.ArrayList, length/0] not found&quot;
          ]
        ]
      ]
    ]
  ]
  </code></pre>
  <p>Edit 2:</p>
  <p>Stored script</p>
  <pre><code>    $params = [
          'id' =&gt; 'add_new_seller_to_existing_doc',
          'body' =&gt; [
              'script' =&gt; [
                  'lang' =&gt; 'painless',
                  'source' =&gt;
                      'if(params.containsKey(\'description\')){
                          if (!ctx._source.containsKey(\'description\')) {
                              ctx._source.description=params.description;
                          } else if (ctx._source.description.length()&lt;params.description.length()) {
                              ctx._source.description=params.description;
                          }
                      }'
              ]
          ]
      ];
  </code></pre>
  <p>Bulk update command:</p>
  <pre><code>    $bulk_obj['body'][] = ['update' =&gt; ['_id' =&gt; $id, '_index' =&gt; 'products']];
  
      $bulk_obj['body'][] = ['id' =&gt; 'add_new_seller_to_existing_doc', 'params' =&gt; ['description'=&gt;'some_description']];
  </code></pre>