Windows10下Docker+Symfony6测试Elasticsearch遇类型转换错误求解释
问题背景
- 系统:Windows 10
- 技术栈:Docker + Symfony 6 + Elasticsearch
- 依赖版本:
"friendsofsymfony/elastica-bundle": "^6.2", "php": ">=8.1", "doctrine/orm": "^2.14", "symfony": "6.2.*"
fos_elastica.yaml 配置
# Read the documentation: https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/setup.md fos_elastica: clients: default: { host: localhost, port: 9209 } indexes: articles: properties: type: ~ name: ~ slug: ~ keyword: ~ persistence: driver: orm model: App\Entity\Article provider: ~ finder: ~
docker-compose.yaml 配置
# elasticsearch —————————————————————————————————————————————————————————————— # elasticsearch server (official image) # https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html elasticsearch: container_name: sb-elasticsearch image: docker.elastic.co/elasticsearch/elasticsearch:6.8.3 # 6.8.4 out ports: - "9209:9200" environment: - "discovery.type=single-node" - "bootstrap.memory_lock=true" - "ES_JAVA_OPTS=-Xms1G -Xmx1G" - "xpack.security.enabled=false" - "http.cors.enabled=true" - "http.cors.allow-origin=*" # elasticsearch head manager (fork of mobz/elasticsearch-head for elasticsearch 6) # /!\ it isn't an official image /!\ # https://hub.docker.com/r/tobias74/elasticsearch-head elasticsearch-head: container_name: sb-elasticsearch-head depends_on: - elasticsearch image: tobias74/elasticsearch-head:6 ports: - "9109:9100"
执行命令报错
执行命令 php bin/console fos:elastica:create 后,出现以下错误:
class java.util.ArrayList cannot be cast to class java.util.Map (java.util.ArrayList and java.util.Map are in module java.base of loader 'bootstrap')
错误原因分析
这个错误是Elasticsearch服务端解析索引创建请求时抛出的类型转换异常,核心问题出在请求格式与Elasticsearch 6.x的要求不匹配:
配置结构不符合规范:你的
fos_elastica.yaml直接在articles索引节点下定义properties,但FOSElasticaBundle 6.x对应Elasticsearch 6.x时,需要将字段属性嵌套在mappings下的文档类型(Elasticsearch 6.x仅支持单一默认类型_doc)中。当前配置生成的请求体是数组(ArrayList)结构,而Elasticsearch 6.x期望的是键值对(Map)格式的映射定义,导致服务端类型转换失败。字段定义不明确:使用
~省略字段类型的写法,可能导致FOSElasticaBundle生成的请求结构不完整,进一步触发解析错误。建议明确指定每个字段的类型(如text、keyword)。
修正后的配置示例:
fos_elastica: clients: default: { host: localhost, port: 9209 } indexes: articles: mappings: _doc: properties: type: { type: text } name: { type: text } slug: { type: keyword } keyword: { type: keyword } persistence: driver: orm model: App\Entity\Article provider: ~ finder: ~
内容的提问来源于stack exchange,提问作者Khaled Boussoffara
相关产品推荐
相关产品推荐

