# How to aggregate runtime\_mappings fields

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-runtime-mappings-fields/313173>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [August 29, 2022, 1:52pm UTC](https://discuss.elastic.co/t/how-to-aggregate-runtime-mappings-fields/313173 "2022-08-29T13:52:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eguenou](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eguenou/32/110207_2.png) [@eguenou](https://discuss.elastic.co/u/eguenou)\
**Post date:** [August 29, 2022, 1:52pm UTC](https://discuss.elastic.co/t/how-to-aggregate-runtime-mappings-fields/313173/1 "2022-08-29T13:52:32Z")

</div>

Hi,  
I'm trying to join two indices using the runtime\_mappings as described [here](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/runtime-retrieving-fields.html#lookup-runtime-fields). The join seems OK however the aggregation always returns 0. Can someone help ? Here is the code :

```auto
POST parent_index/_search
{
  "size": 0,
  "runtime_mappings": {
    "my_mapping": {
      "type": "lookup",
      "target_index": "child_index",
      "input_field": "parent_key",
      "target_field": "child_foreign_key",
      "fetch_fields": [
        "id",
        "sales",
      ]
    }
  },
  "aggregations": {
    "per_id": {
      "terms": {
        "field": "id.keyword"
      },
      "aggregations": {
        "per_sales": {
          "sum": {
            "field": "my_mapping.sales"
          }
        }
      }
    }
  },
  "_source": false
}

```

I have 2 parent documents and 2 child documents. The two child documents are linked to one parent therefore one parent should have a total of 0 'per\_sales' and the other parent more than 0 in the 'per\_sales' aggregation.

I got this result which is not correct:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "per_id": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "parent_key_1",
          "doc_count": 1,
          "per_sales": {
            "value": 0
          }
        },
        {
          "key": "parent_key_2",
          "doc_count": 1,
          "per_sales": {
            "value": 0
          }
        }
      ]
    }
  }
}

```

PS: I'm using Elasticsearch 8.4 on Ubuntu 20.04.  
Thanks

---

<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:** [September 26, 2022, 1:53pm UTC](https://discuss.elastic.co/t/how-to-aggregate-runtime-mappings-fields/313173/2 "2022-09-26T13:53:17Z")

</div>

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