# Runtime field not appearing in search results

**URL:** <https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [February 13, 2023, 9:46pm UTC](https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425 "2023-02-13T21:46:25Z")\
**Posts on this page:** 4\
**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:** [February 13, 2023, 9:46pm UTC](https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425/1 "2023-02-13T21:46:25Z")

</div>

I'm trying to set up a run time field to calculate the total of an e-commerce purchase. But there is something wrong with the way I'm declaring run time field, and it might have something to do with nested objects.

First I set up the index , the mapping and the runtime field like this:

```auto
PUT purchase
PUT purchase/_mapping
{
  "properties": {
    "items": {
      "type": "nested",
      "properties": {
        "product_id": {
          "type": "long"
        },
        "quantity": {
          "type": "long"
        },
        "product": {
          "properties": {
            "name": {
              "type": "keyword"
            },
            "product_id": {
              "type": "long"
            },
            "price": {
              "type": "long"
            }
          }
        }
      }
    },
    "purchase_date": {
      "type": "date"
    }
  },
  "runtime": {
    "items.total": {
      "type": "long",
      "script": {
        "source": "emit(doc['items.quantity'].value * doc['items.product.price'].value)"
      }
    }
  }
}

```

Then I post one test record lie this:

```auto
POST purchase/_doc
{
  "purchase_date": "2023-01-01",
  "items": [
    {
      "quantity": 2,
      "product_id": 1,
      "product": {
        "product_id": 1,
        "name": "pencil",
        "price": 1
      }
    },
    {
      "quantity": 4,
      "product_id": 2,
      "product": {
        "product_id": 2,
        "name": "stapler",
        "price": 5
      }
    }
  ]
}

```

When I do a `GET purchase/_search`, I get a result like this:

```auto
{
          "purchase_date": "2023-01-01",
          "items": [
            {
              "quantity": 2,
              "product_id": 1,
              "product": {
                "product_id": 1,
                "name": "pencil",
                "price": 1
              }
            },
            {
              "quantity": 4,
              "product_id": 2,
              "product": {
                "product_id": 2,
                "name": "stapler",
                "price": 5
              }
            }
          ]
        }

```

I was expecting a field `order_items.total` to be a sibling field to `order_items.quantity`. I've tried quite a few things but nothing seems to get the `order_items.total` field to appear.

What did I do wrong?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 14, 2023, 12:03am UTC](https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425/2 "2023-02-14T00:03:59Z")

</div>

Hi @learningelastic

Read this [answer](https://discuss.elastic.co/t/runtime-field-create-nested-object/312316/2), maybe dont work to nested fields.

> [@learningelastic](#):
>
> I was expecting a field `order_items.total` to be a sibling field to `order_items.quantity`. I've tried quite a few things but nothing seems to get the `order_items.total` field to appear.

Doc:

> Use the [`fields`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-fields.html) parameter on the `_search` API to retrieve the values of runtime fields. Runtime fields won’t display in `_source` , but the `fields` API works for all fields, even those that were not sent as part of the original `_source` .

---

<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:** [February 14, 2023, 12:54am UTC](https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425/3 "2023-02-14T00:54:00Z")

</div>

Thanks, you might be right that runtime fields don't work on nested objects. I just ran this search query:

```auto
GET purchase/_search
{
  "fields":["items.total"]
}

```

And I got this result:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:92)",
          "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:110)",
          "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:105)",
          "emit(doc['items.quantity'].value * doc['items.product.price'].value)",
          " ^---- HERE"
        ],
        "script": "emit(doc['items.quantity'].value * doc['items.product.price'].value)",
        "lang": "painless",
        "position": {
          "offset": 26,
          "start": 0,
          "end": 68
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "purchase",
        "node": "VYLFHFZ3RtOtR_P8dlnx3w",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:92)",
            "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:110)",
            "org.elasticsearch.server@8.6.1/org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:105)",
            "emit(doc['items.quantity'].value * doc['items.product.price'].value)",
            " ^---- HERE"
          ],
          "script": "emit(doc['items.quantity'].value * doc['items.product.price'].value)",
          "lang": "painless",
          "position": {
            "offset": 26,
            "start": 0,
            "end": 68
          },
          "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!"
          }
        }
      }
    ]
  },
  "status": 400
}

```

If I change my mapping to delete the `"type":"nested"` line, then I run the same

```auto
GET purchase/_search
{
  "fields":["items.total"]
}

```

Then I do see

```auto
"fields": {
          "items.total": [
            2
          ]
        }

```

Which seems like elasticsearch just picked ONE record from the `items` array and multiplied `product.price` with the `quantity`.

---

<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:** [March 14, 2023, 12:55am UTC](https://discuss.elastic.co/t/runtime-field-not-appearing-in-search-results/325425/4 "2023-03-14T00:55:01Z")

</div>

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