Elasticsearch.Net 7/NEST 7创建索引返回404无效响应问题
问题排查与解决方案
环境与复现代码
当前运行环境为Elasticsearch 7.7 + NEST 7.7,NuGet包引用配置:
<PackageReference Include="Elasticsearch.Net" Version="7.7.0" /> <PackageReference Include="NEST" Version="7.7.0" />
索引创建逻辑代码:
var connectionPool = new SingleNodeConnectionPool(new Uri(elasticUrl)); var settings = new ConnectionSettings(connectionPool) .BasicAuthentication(elasticUsername, elasticPassword) .DeadTimeout(TimeSpan.FromSeconds(300)) .DisableDirectStreaming() .DefaultIndex("demo_index"); var client = new ElasticClient(settings); var response = client.Indices.Create("eagle-Dev-products", index => index.Map<ElasticSimpleProduct>( x => x.AutoMap() )); return response.DebugInformation;
映射模型定义:
[ElasticsearchType(RelationName = "searchproduct")] public class ElasticSimpleProduct { [Number(DocValues = false, IgnoreMalformed = true, Coerce = true)] public long Id { get; set; } [Keyword] public int ProductId { get; set; } [Text] public string PartNumber { get; set; } [Text] public string Name { get; set; } [Text] public string MetaKeywords { get; set; } [Text] public string MetaDescription { get; set; } [Text] public string MetaTitle { get; set; } [Text] public string ShortDescription { get; set; } [Text] public string FullDescription { get; set; } }
执行代码后返回404错误,错误信息如下:
Invalid NEST response built from a successful (404) low level call on PUT: /eagle-Dev-products # Audit trail of this API call: - [1] HealthyResponse: Node: https://eagle-demo.kb.us-central1.gcp.cloud.es.io:9243/ Took: 00:00:13.8974690 # Request: {"mappings":{"properties":{"id":{"coerce":true,"ignore_malformed":true,"doc_values":false,"type":"float"},"productId":{"type":"keyword"}, "partNumber":{"type":"text"},"name":{"type":"text"},"metaKeywords":{"type":"text"},"metaDescription":{"type":"text"},"metaTitle":{"type":"text"},"shortDescription":{"type":"text"},"fullDescription":{"type":"text"}}}} # Response: {"statusCode":404,"error":"Not Found","message":"Not Found"}
根因定位与修复步骤
404错误的核心原因是请求没有打到正确的Elasticsearch API路由,按以下顺序排查修复:
- 校验服务端点配置
你使用的是GCP上的Elastic Cloud服务,首先核对elasticUrl的取值:- 不要混用Kibana端点和Elasticsearch端点,Elastic Cloud控制台会单独列出两个服务的访问地址,Kibana服务不接受索引创建类API请求,调用就会返回404
- 不要在地址后手动拼接多余路径后缀,直接使用控制台给出的完整Elasticsearch端点即可
- 验证方法:用curl直接请求配置的端点地址,携带基础认证信息,能正常返回集群版本、名称等信息才说明地址正确:
curl -u 你的用户名:你的密码 https://配置的ES端点地址
elasticUrl配置即可。 - 修正模型映射的错误配置
现有模型类存在两个映射逻辑错误,即使地址连通也会导致字段类型不符合预期:Id字段声明为long类型,但Number特性未指定数值类型,NEST AutoMap默认会将未指定类型的Number字段映射为float,需要修改特性为[Number(NumberType.Long, DocValues = false, IgnoreMalformed = true, Coerce = true)]ProductId字段为int类型,标记[Keyword]特性不符合映射规则:Keyword是字符串字段类型,数值类型字段会被NEST自动识别为integer,要么将字段类型改为string,要么去掉[Keyword]特性使用自动映射。- 模型类末尾多了一个多余的闭合大括号
},属于语法错误,编译阶段就会报错,直接删除即可。
- 校验账号权限
确认配置的elasticUsername对应账号拥有索引创建权限,Elastic Cloud默认的elastic超级管理员账号拥有完整权限,如果使用自定义账号,需要确认绑定的角色包含create_index操作权限。 - 增加调试逻辑
修正配置后可以在代码中增加响应校验逻辑,快速定位后续可能出现的问题:if (!response.IsValid) { // 输出服务端返回的具体错误原因 Console.WriteLine($"创建索引失败:{response.ServerError?.Error?.Reason}"); }
内容的提问来源于stack exchange,提问作者Isanka Thalagala
相关产品推荐
相关产品推荐

