{
  "id": "elasticsearch/runtime-field-context-error",
  "signature": "ElasticsearchException: runtime field [my_field] cannot be used in context [query] because it is defined as [script] type",
  "signature_zh": "ElasticsearchException: 运行时字段[my_field]不能在上下文中使用[query]，因为它定义为[script]类型",
  "regex": "ElasticsearchException: runtime field \\[.*\\] cannot be used in context \\[.*\\] because it is defined as \\[.*\\] type",
  "domain": "elasticsearch",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A runtime field with a `script` type is used in a context that expects a different type (e.g., `keyword` or `long`), causing a type mismatch.",
  "root_cause_type": "generic",
  "root_cause_zh": "运行时字段的`script`类型被用于期望其他类型（如`keyword`或`long`）的上下文，导致类型不匹配。",
  "versions": [
    {
      "version": "Elasticsearch 7.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Elasticsearch 8.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Elasticsearch 8.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Changing the runtime field type to `keyword` without adjusting the script logic",
      "why_fails": "The script may output incompatible values (e.g., numbers) for a keyword field, leading to further errors.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "Removing the runtime field and using a concrete mapping instead",
      "why_fails": "This requires reindexing, which is disruptive and may not be feasible in production.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting `ignore_malformed: true` on the index to bypass type errors",
      "why_fails": "This only applies to concrete fields, not runtime fields, and will not resolve the context mismatch.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Update the runtime field script to return the correct type: `PUT my_index/_mapping {\"runtime\": {\"my_field\": {\"type\": \"keyword\", \"script\": {\"source\": \"emit(doc['source_field'].value.toString())\"}}}}`",
      "success_rate": 0.85,
      "how": "Update the runtime field script to return the correct type: `PUT my_index/_mapping {\"runtime\": {\"my_field\": {\"type\": \"keyword\", \"script\": {\"source\": \"emit(doc['source_field'].value.toString())\"}}}}`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a painless script transformation in the query instead of a runtime field: `GET my_index/_search {\"query\": {\"script_score\": {\"script\": {\"source\": \"doc['source_field'].value.length()\"}}}}`",
      "success_rate": 0.8,
      "how": "Use a painless script transformation in the query instead of a runtime field: `GET my_index/_search {\"query\": {\"script_score\": {\"script\": {\"source\": \"doc['source_field'].value.length()\"}}}}`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "更新运行时字段脚本以返回正确类型：`PUT my_index/_mapping {\"runtime\": {\"my_field\": {\"type\": \"keyword\", \"script\": {\"source\": \"emit(doc['source_field'].value.toString())\"}}}}`",
    "在查询中使用Painless脚本转换代替运行时字段：`GET my_index/_search {\"query\": {\"script_score\": {\"script\": {\"source\": \"doc['source_field'].value.length()\"}}}}`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime-search-request.html",
  "official_doc_section": null,
  "error_code": "RUNTIME_FIELD_CONTEXT_MISMATCH",
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}