# Runtime field causes the error "A document doesn't have a field"

**URL:** <https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [May 8, 2023, 11:08pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848 "2023-05-08T23:08:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningelastic](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@learningelastic](https://discuss.elastic.co/u/learningelastic)\
**Post date:** [May 8, 2023, 11:08pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848/1 "2023-05-08T23:08:18Z")

</div>

I'm trying to use a run time field but I keep getting the error:

```auto
A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!

```

You can reproduce the issue by running this script

```auto
PUT player
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "create_date": {
        "type": "date",
        "format": "yyyy-MM-dd"
      },
      "achievements": {
        "type": "nested",
        "properties": {
          "quest": {
            "type": "keyword"
          },
          "points": {
            "type": "long"
          },
          "completion_date": {
            "type": "date",
            "format": "yyyy-MM-dd"
          }
        }
      }
    }
  }
}
POST player/_doc
{
  "name": "Billy Dragon",
  "create_date": "2023-01-01",
  "achievements": [
    {
      "quest":"Streets of Desolation",
      "points": 20,
      "completion_date": "2023-02-01"
    }
  ]
}
GET player/_search
{
  "runtime_mappings": {
    "new_field": {
      "type": "long",
      "script": {
        "source": """
          long current = (new Date()).getTime();
          long dueDate = doc['achievements.completion_date'].value.getMillis();
          long days = Math.round((current - dueDate)/(1000*3600*24));
          emit(days);
          """
      }
    }
  },
  "aggs": {
    "new_field": {
      "terms": {
        "field": "new_field"
      }
    }
  }
}

```

All this script does is create a new index, then insert one record, then do a search with run time field. Once you do the search, you will see the error I mentioned.

What did I do wrong?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 9, 2023, 3:36pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848/2 "2023-05-09T15:36:17Z")

</div>

Hi @learningelastic

` "type": "nested",`

Nested Field and runtime are much harder not directly supported by painless you have to loop through them etc with a much more complex script... is that what you really want?  
Are you going to have an array of achievements?  
If so which one do you want to access in the runtime field? All of them? The First One? The Last One?  
I know this is an example but what are you really trying to accomplish?

Say the document looks like

```auto
POST player/_doc
{
  "name": "Billy Dragon",
  "create_date": "2023-01-01",
  "achievements": [
    {
      "quest": "Streets of Desolation",
      "points": 20,
      "completion_date": "2023-02-01"
    },
    {
      "quest": "Streets of Fire",
      "points": 35,
      "completion_date": "2023-01-02"
    }
  ]
}

```

What are you trying to do?

---

<div class="post-metadata">

**Author:** ![learningelastic](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@learningelastic](https://discuss.elastic.co/u/learningelastic)\
**Post date:** [May 9, 2023, 4:33pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848/3 "2023-05-09T16:33:38Z")

</div>

Thanks for reply Stephen!

One of the queries I wanted to run and visualize in Kibana is something like:

```auto
Graph Type: Horizontal Bar
Y Axis : Number of Days since Achievement.completion_date
X Axis: Concat(player.name, achievement[N].quest)
Filter: WHERE player.create_date >= 2023-01 && player.create_date < 2023-02-01

```

So the part I'm trying to figure out is how to loop through every achievement and subtract its `completion_date` from `(new Date()).getTime()`.

Also, a bit later, I might want to apply a "compound interest" formula to the achievement.points , then plot y-axis as "compound interest points" and x axis is `concat(player.name, achievement[x].quest)`

Do you have suggestion on what I need to study? or any code examples that I can learn from?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [May 9, 2023, 8:34pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848/4 "2023-05-09T20:34:35Z")

</div>

In short, I would not use nested type. Nested type "looks good" and solves some problems but creates others... I would denormalize the data.

Are you going to be updating this data? ... adding achievements etc?

> [@learningelastic](#):
>
> So the part I'm trying to figure out is how to loop through every achievement and subtract its `completion_date` from `(new Date()).getTime()`.

Yeah perfect example ... very hard / normal full programming a simple runtime field will not work.

Simple Way is This

```auto
POST player/_doc
{
  "name": "Billy Dragon",
  "create_date": "2023-01-01",
  "achievements": [
    {
      "quest": "Streets of Desolation",
      "points": 20,
      "completion_date": "2023-02-01"
    },
    {
      "quest": "Streets of Fire",
      "points": 35,
      "completion_date": "2023-01-02"
    }
  ]
}

```

Becomes this

```auto
POST player/_doc
{
  "name": "Billy Dragon",
  "create_date": "2023-01-01",
  "achievements": 
    {
      "quest": "Streets of Desolation",
      "points": 20,
      "completion_date": "2023-02-01"
    }
}

POST player/_doc
{
  "name": "Billy Dragon",
  "create_date": "2023-01-01",
  "achievements": 
   {
      "quest": "Streets of Fire",
      "points": 35,
      "completion_date": "2023-01-02"
    }
}

```

Then everything will be much easier

---

<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:** [June 6, 2023, 8:35pm UTC](https://discuss.elastic.co/t/runtime-field-causes-the-error-a-document-doesnt-have-a-field/332848/5 "2023-06-06T20:35:18Z")

</div>

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