# Unable to create scripted field

**URL:** <https://discuss.elastic.co/t/unable-to-create-scripted-field/252028>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [October 14, 2020, 10:55am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028 "2020-10-14T10:55:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_Angel\_Morena\_Si](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jose_angel_morena_si/32/76522_2.png) [@Jose\_Angel\_Morena\_Si](https://discuss.elastic.co/u/Jose_Angel_Morena_Si)\
**Post date:** [October 14, 2020, 10:55am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/1 "2020-10-14T10:55:32Z")

</div>

Hello,

**Elastic version** : 7.9.1  
**Kibana version** : 7.9.0

I am struggling to create a scripted field. To put it on perspective, I have the field "apache.status.total\_kbytes". And I would like to create a scripted field named "apache.status.total\_mbytes".

Mapping of apache.status.total\_kbytes:

```auto
  "metricbeat-7.9.2-2020.10.14-000001" : {
    "mappings" : {
      "apache.status.total_kbytes" : {
        "full_name" : "apache.status.total_kbytes",
        "mapping" : {
          "total_kbytes" : {
            "type" : "long"
          }
        }
      }
    }
  }

```

In order to do so, I created a scriped field in kibana, using the following code:

```auto
if (doc.containsKey('apache.status.total_kbytes')){
    long kb=doc['apache.status.total_kbytes'].value;
    return kb/1024L;
}
return null;

```

When I enter into kibana I see the following error

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/d/4ded8b852030cda621e0634b4063d29449b71ed9.png)

Am I missing something? Why can't I access the "value" attribute of the document field?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [October 14, 2020, 11:05am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/2 "2020-10-14T11:05:25Z")

</div>

I guess something like this may help

```
if (doc.containsKey('apache.status.total_kbytes') && doc['apache.status.total_kbytes'].size() != 0){
    long kb=doc['apache.status.total_kbytes'].value;
    return kb/1024L;
}

```

The full error was : A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!

```
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
    "kb=doc['apache.status.total_kbytes'].value;\r\n ",
    " ^---- HERE"
   ],
   "script": "if (doc.containsKey('apache.status.total_kbytes')){\r\n long kb=doc['apache.status.total_kbytes'].value;\r\n return kb/1024L;\r\n}\r\nreturn null;",
   "lang": "painless",
   "position": {
    "offset": 98,
    "start": 62,
    "end": 111
   }
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "metricbeat-7.9.2-2020.09.26-000001",
   "node": "1ksZhfyPR2my8OyC5uuVSQ",
   "reason": {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
     "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:121)",
     "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:115)",
     "kb=doc['apache.status.total_kbytes'].value;\r\n ",
     " ^---- HERE"
    ],
    "script": "if (doc.containsKey('apache.status.total_kbytes')){\r\n long kb=doc['apache.status.total_kbytes'].value;\r\n return kb/1024L;\r\n}\r\nreturn null;",
    "lang": "painless",
    "position": {
     "offset": 98,
     "start": 62,
     "end": 111
    },
    "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!"
    }
   }
  }
 ]
}
return null;
```

---

<div class="post-metadata">

**Author:** ![Jose\_Angel\_Morena\_Si](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jose_angel_morena_si/32/76522_2.png) [@Jose\_Angel\_Morena\_Si](https://discuss.elastic.co/u/Jose_Angel_Morena_Si)\
**Post date:** [October 14, 2020, 11:13am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/3 "2020-10-14T11:13:56Z")

</div>

Okay it worked. My question now would be... why do I need to add the condition `doc['apache.status.total_kbytes'].size() != 0` if I already check if the field exists within the dictionary via the condition: `doc.containsKey('apache.status.total_kbytes')`

Why do I have to do it explicitely like this `doc['apache.status.total_kbytes'].size() != 0`

I hope I explained myself.

Thanks!

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [October 14, 2020, 11:22am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/4 "2020-10-14T11:22:24Z")

</div>

The better is to use `if (!doc['apache.status.total_kbytes'].empty) { ... }`

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [October 14, 2020, 11:32am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/5 "2020-10-14T11:32:00Z")

</div>

The breaking change was mentionned [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#_accessing_missing_document_values_will_throw_an_error)

---

<div class="post-metadata">

**Author:** ![Jose\_Angel\_Morena\_Si](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jose_angel_morena_si/32/76522_2.png) [@Jose\_Angel\_Morena\_Si](https://discuss.elastic.co/u/Jose_Angel_Morena_Si)\
**Post date:** [October 14, 2020, 11:43am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/6 "2020-10-14T11:43:52Z")

</div>

Thank you very much Yassine!

---

<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:** [November 11, 2020, 11:43am UTC](https://discuss.elastic.co/t/unable-to-create-scripted-field/252028/7 "2020-11-11T11:43:52Z")

</div>

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