# Runtime Mapping agg query error

**URL:** <https://discuss.elastic.co/t/runtime-mapping-agg-query-error/364883>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields\
**Created:** [August 14, 2024, 6:32am UTC](https://discuss.elastic.co/t/runtime-mapping-agg-query-error/364883 "2024-08-14T06:32:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [August 14, 2024, 6:32am UTC](https://discuss.elastic.co/t/runtime-mapping-agg-query-error/364883/1 "2024-08-14T06:32:58Z")

</div>

Can you help me the understand the meaning of this error. Added query, response and index mapping  
Query:

```auto
{
    "runtime_mappings": {
        "count_defect": {
            "type": "long",
            "script": "emit(doc['defect'].size());"
        }
    },
    "size": 0,
    "aggs": {
        "defect_mapping": {
            "terms": {
                "field": "count_defect"
            }
        }
    }
}

```

Error Response:

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
                    "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
                    "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
                    "emit(doc['source']['defect'].size());",
                    " ^---- HERE"
                ],
                "script": "emit(doc['source']['defect'].size());",
                "lang": "painless",
                "position": {
                    "offset": 9,
                    "start": 0,
                    "end": 37
                }
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "poc_scenario_v1",
                "node": "d6o5XrfZS7OqkpZAfVl9XA",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
                        "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
                        "org.elasticsearch.server@8.8.2/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
                        "emit(doc['source']['defect'].size());",
                        " ^---- HERE"
                    ],
                    "script": "emit(doc['source']['defect'].size());",
                    "lang": "painless",
                    "position": {
                        "offset": 9,
                        "start": 0,
                        "end": 37
                    },
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [source] in mapping"
                    }
                }
            }
        ]
    },
    "status": 400
}

```

Index Mapping:

```auto
{
    "poc_scenario_v1": {
        "mappings": {
            "dynamic": "false",
            "properties": {
                "_meta": {
                    "properties": {
                        "conf_entity_metadata": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "conf_entity_status": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "conf_metadata": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "ext_int_work_item": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "metadata_value": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "project": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "scn_reln_scenario_ext_int_work_item": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        },
                        "users": {
                            "properties": {
                                "pid": {
                                    "type": "integer"
                                }
                            }
                        }
                    }
                },
                "created_timestamp": {
                    "type": "date"
                },
                "custom_field": {
                    "type": "nested",
                    "properties": {
                        "bool_value": {
                            "type": "integer"
                        },
                        "conf_entity_metadata_pid": {
                            "type": "integer"
                        },
                        "date_value": {
                            "type": "date"
                        },
                        "db_column_name": {
                            "type": "keyword",
                            "index": false
                        },
                        "is_required": {
                            "type": "integer",
                            "index": false
                        },
                        "label": {
                            "type": "text",
                            "index": false
                        },
                        "md_type": {
                            "type": "integer",
                            "index": false
                        },
                        "metadata_value_pid": {
                            "type": "integer"
                        },
                        "pid": {
                            "type": "integer"
                        },
                        "text_value": {
                            "type": "keyword"
                        },
                        "unique_name": {
                            "type": "keyword"
                        }
                    }
                },
                "defect": {
                    "type": "nested",
                    "properties": {
                        "aq_note": {
                            "type": "text",
                            "index": false
                        },
                        "associated_timestamp": {
                            "type": "date"
                        },
                        "key": {
                            "type": "keyword"
                        },
                        "last_refresh_timestamp": {
                            "type": "date"
                        },
                        "link": {
                            "type": "text",
                            "index": false
                        },
                        "pid": {
                            "type": "integer"
                        },
                        "project": {
                            "type": "keyword"
                        },
                        "status": {
                            "type": "keyword"
                        },
                        "test_job_reln_defect_item_pid": {
                            "type": "integer"
                        },
                        "title": {
                            "type": "text"
                        }
                    }
                },
                "deleted_timestamp": {
                    "type": "date"
                },
                "entity_type": {
                    "type": "keyword"
                },
                "is_checkout_entity": {
                    "type": "integer"
                },
                "is_deleted": {
                    "type": "integer"
                },
                "last_modified_timestamp": {
                    "type": "date"
                },
                "name": {
                    "type": "keyword"
                },
                "pid": {
                    "type": "integer"
                },
                "project": {
                    "properties": {
                        "deleted_timestamp": {
                            "type": "date"
                        },
                        "is_deleted": {
                            "type": "integer"
                        },
                        "pid": {
                            "type": "integer"
                        },
                        "project_display_name": {
                            "type": "keyword"
                        },
                        "project_name": {
                            "type": "keyword",
                            "index": false
                        }
                    }
                },
                "scm_base_pid": {
                    "type": "integer"
                },
                "status": {
                    "properties": {
                        "category": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "pid": {
                            "type": "integer"
                        }
                    }
                },
                "traceability": {
                    "type": "nested",
                    "properties": {
                        "aq_note": {
                            "type": "text",
                            "index": false
                        },
                        "key": {
                            "type": "keyword"
                        },
                        "last_refresh_timestamp": {
                            "type": "date"
                        },
                        "link": {
                            "type": "text",
                            "index": false
                        },
                        "pid": {
                            "type": "integer"
                        },
                        "project": {
                            "type": "keyword"
                        },
                        "scn_reln_scenario_ext_int_work_item_pid": {
                            "type": "integer"
                        },
                        "status": {
                            "type": "keyword"
                        },
                        "title": {
                            "type": "text"
                        }
                    }
                },
                "user_created": {
                    "properties": {
                        "is_deleted": {
                            "type": "integer"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "pid": {
                            "type": "integer"
                        }
                    }
                },
                "user_last_modified": {
                    "properties": {
                        "is_deleted": {
                            "type": "integer"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "pid": {
                            "type": "integer"
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Musab\_Dogan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/musab_dogan/32/70691_2.png) [@Musab\_Dogan](https://discuss.elastic.co/u/Musab_Dogan)\
**Post date:** [August 14, 2024, 2:41pm UTC](https://discuss.elastic.co/t/runtime-mapping-agg-query-error/364883/2 "2024-08-14T14:41:27Z")

</div>

The error looks related to `nested` field type. There are a couple of workarounds for that. Sharing a similar discussions below.

1. [How to access objects fields nested value in runtime field painless (or expression) scripts?](https://discuss.elastic.co/t/how-to-access-objects-fields-nested-value-in-runtime-field-painless-or-expression-scripts/302699)
2. [How to create an array runtime field - #10 by cr\_168328](https://discuss.elastic.co/t/how-to-create-an-array-runtime-field/322791/10)
3. [kibana - How to create an array type runtime field based on a nested field in elasticsearch? - Stack Overflow](https://stackoverflow.com/questions/75066316/how-to-create-an-array-type-runtime-field-based-on-a-nested-field-in-elasticsear)
