Search nested fields

I create index with nested field

{
    "chat": {
        "aliases": {},
        "mappings": {
            "properties": {
                "dataset_id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "messages": {
                    "properties": {
                        "chat_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "dialog_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "index": {
                            "type": "long"
                        },
                        "role": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "row_number": {
                            "type": "long"
                        },
                        "trainable": {
                            "type": "boolean"
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "chat",
                "creation_date": "1751372790348",
                "number_of_replicas": "1",
                "uuid": "67iZTI7IT8ehAqr7k7ZetA",
                "version": {
                    "created": "9009000"
                }
            }
        }
    }
}```

and trying to search messages from user

{
"query": {
"nested": {
"path": "messages",
"query": {
"bool": {
"must": [
{
"match": {
"messages.role.keyword": "user"
}
}
]
}
}
}
}
}```

but I've got error

{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: [nested] failed to find nested object under path [messages]",
                "index_uuid": "67iZTI7IT8ehAqr7k7ZetA",
                "index": "chat"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "chat",
                "node": "lwXfQmtGQlu5e8aDxK1xYw",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: [nested] failed to find nested object under path [messages]",
                    "index_uuid": "67iZTI7IT8ehAqr7k7ZetA",
                    "index": "chat",
                    "caused_by": {
                        "type": "illegal_state_exception",
                        "reason": "[nested] failed to find nested object under path [messages]"
                    }
                }
            }
        ]
    },
    "status": 400
}

There is a difference between nested fields and nested mapping type. The nested query type assumes use of the nested mapping type, which you do not use in the mappings you showed us.

this is my schema

{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "match_mapping_type": "string",
          "path_match": "messages.*",
          "mapping": {
            "type": "text",
            "copy_to": "content"
          }
        }
      }
    ],
    "properties": {
      "id": {
        "type": "keyword"
      },
      "dataset_id": {
        "type": "keyword"
      },
      "messages": {
        "type": "nested"
      },
      "content": {
        "type": "text"
      },
      "row_number": {
        "type": "integer"
      }
    }
  }
}```

Have a look at the docs I linked to. You do not have any nested mappings so the nested query is not appropriate.