# \[SOLVED\] Issue with runtime field

**URL:** <https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [December 6, 2022, 6:26pm UTC](https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618 "2022-12-06T18:26:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [December 6, 2022, 6:26pm UTC](https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618/1 "2022-12-06T18:26:29Z")

</div>

If create a runtime field for searches with aggregations.

```auto
PUT /work-analyzers/_mapping
{
  "runtime": {
    "total_consumption": {
      "type": "double",
      "script": {
        "source": """
        emit(doc['pa1_w'].value + doc['pa2_w'].value + doc['pa3_w'].value)
        """
      }
    }
  }
}

```

Work fine but if anytime search when the search ever reaches a document that has (I assume) null values in all three fields that make up the runtime field, an exception is thrown.

I have looked for the output, but in all it fails, when executing the creation of the calculated field, and I can't find the topic `doc[<field>].size()==0`

```auto
"caused_by": {
            "type": "illegal_state_exception",
            "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
}

```

### Search fails with size 100 (not with 10 or 30)

```auto
GET /work-analyzers/_search
{
  "size": 100,
  "query": {
    "range": {
      "total_consumption": {
        "gt": 700,
        "boost": 1
      }
    }
  },
  "_source": false,
  "fields": [
    {
      "field": "pa1_w"
    },
    {
      "field": "pa2_w"
    },
    {
      "field": "pa3_w"
    },
    {
      "field": "total_consumption"
    }
  ],
  "sort": [
    {
      "_doc": {
        "order": "asc"
      }
    }
  ],
  "track_total_hits": -1
}

```

Apreciate help.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [December 6, 2022, 6:41pm UTC](https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618/2 "2022-12-06T18:41:19Z")

</div>

Hi Abdelkarim,

The `emit` function doesn't cannot accept nulls, [as per the docs](https://www.elastic.co/guide/en/elasticsearch/painless/8.5/painless-runtime-fields-context.html).

I would recommend adding a null check for your values prior to `emit`. You can use either the null safe operator or and if statement:

1. [Null operator](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-reference.html#null-safe-operator)
2. [if statement](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-statements.html)

---

<div class="post-metadata">

**Author:** ![abkrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abkrim/32/20217_2.png) [@abkrim](https://discuss.elastic.co/u/abkrim)\
**Post date:** [December 6, 2022, 7:27pm UTC](https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618/3 "2022-12-06T19:27:42Z")

</div>

Hi

I knew the issue apart from the fact that the error message makes it somewhat clear. What he didn't know was how to apply verification to the painles script. After some tests, I found it.

I get a solution but I do not like it very much.

```auto
PUT /work-analyzers/_mapping
{
  "runtime": {
    "total_consumption": {
      "type": "double",
      "script": {
        "lang": "painless",
        "source": """
          double sum = 0;
          if (doc['pa1_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa1_w'].value}
          if (doc['pa2_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa2_w'].value}
          if (doc['pa3_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa3_w'].value}
          emit(sum);
        """
      }
    }
  }
}

```

I have tested it by going through the query in sections of 1000 docs and it does not fail.

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 3, 2023, 7:28pm UTC](https://discuss.elastic.co/t/solved-issue-with-runtime-field/320618/4 "2023-01-03T19:28:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
