Runtime Mapping agg query error

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

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

Error Response:

{
    "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:

{
    "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"
                        }
                    }
                }
            }
        }
    }
}

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?
  2. How to create an array runtime field - #10 by cr_168328
  3. kibana - How to create an array type runtime field based on a nested field in elasticsearch? - Stack Overflow