title: Elasticsearch Query DSL: Length of field, if field exists
body: <p>Say I have a field, <code>data.url</code>. Some our logs contain this field, some do not. I want to return only results where <code>data.url</code> is more than, say, 50 characters long. Really I just need a list of URLs.</p>
  <p>I'm trying:</p>
  <pre><code>GET _search
  {
    &quot;query&quot;: {
      &quot;bool&quot;: {
        &quot;filter&quot;: {
          &quot;script&quot;: {
            &quot;script&quot;: {
              &quot;source&quot;: &quot;doc['data.url'].value.length() &gt; 50&quot;,
              &quot;lang&quot;: &quot;painless&quot;
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p>But get mixed errors:</p>
  <pre><code>{
    &quot;error&quot; : {
      &quot;root_cause&quot; : [
        {
          &quot;type&quot; : &quot;script_exception&quot;,
          &quot;reason&quot; : &quot;runtime error&quot;,
          &quot;script_stack&quot; : [
            &quot;org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:90)&quot;,
            &quot;org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)&quot;,
            &quot;doc['data.url'].value.length() &gt; 50&quot;,
            &quot;    ^---- HERE&quot;
          ],
          &quot;script&quot; : &quot;doc['data.url'].value.length() &gt; 50&quot;,
          &quot;lang&quot; : &quot;painless&quot;,
          &quot;position&quot; : {
            &quot;offset&quot; : 4,
            &quot;start&quot; : 0,
            &quot;end&quot; : 35
          }
        },
  </code></pre>
  <p>or</p>
  <pre><code>        &quot;type&quot; : &quot;script_exception&quot;,
          &quot;reason&quot; : &quot;runtime error&quot;,
          &quot;script_stack&quot; : [
            &quot;org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:496)&quot;,
            &quot;org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:503)&quot;,
            &quot;doc['data.url'].value.length() &gt; 50&quot;,
            &quot;               ^---- HERE&quot;
          ],
          &quot;script&quot; : &quot;doc['data.url'].value.length() &gt; 50&quot;,
          &quot;lang&quot; : &quot;painless&quot;,
          &quot;position&quot; : {
            &quot;offset&quot; : 15,
            &quot;start&quot; : 0,
            &quot;end&quot; : 35
          }
  </code></pre>
  <p>With</p>
  <pre><code>          &quot;caused_by&quot; : {
              &quot;type&quot; : &quot;illegal_argument_exception&quot;,
              &quot;reason&quot; : &quot;No field found for [data.url] in mapping with types []&quot;
            }
  </code></pre>
  <p>and sometimes</p>
  <pre><code>          &quot;caused_by&quot; : {
              &quot;type&quot; : &quot;illegal_state_exception&quot;,
              &quot;reason&quot; : &quot;A document doesn't have a value for a field! Use doc[&lt;field&gt;].size()==0 to check if a document is missing a field!&quot;
            }
  </code></pre>
  <p>This field definitely exists; I can see it in the logs, search in the search field, and using term works:</p>
  <pre><code>GET _search
  {
    &quot;query&quot;: {
      &quot;bool&quot;: {
        &quot;filter&quot;: {
          &quot;term&quot;: {
            &quot;data.url&quot;: &quot;www.google.com&quot;
          }
        }
      }
    }
  }
  </code></pre>
  <p>What am I missing?</p>
  <p>I'm using Elasticsearch 7.8.</p>