首页 > 解决方案 > ElasticSearch 索引名称为空

问题描述

System.ArgumentException: 
    Index name is null for the given type and no default index is set. 
    Map an index name using ConnectionSettings.DefaultMappingFor<TDocument>()
    or set a default index using ConnectionSettings.DefaultIndex().

错误信息在上面!

var url = configuration["elasticsearch:url"];
var defaultIndex = configuration["elasticsearch:index"];
var settings = new ConnectionSettings(new Uri(url)).DefaultMappingFor<MachineConnectionInformation>(x=> x.IndexName(defaultIndex));
                
            
var client = new ElasticClient(settings);           
client.Indices.Create("machine", index => index.Map<MachineConnectionInformation>(x => x.AutoMap()));
services.AddSingleton(client);

我的扩展代码在上面!

var response = _client.IndexDocument(model);  //this is where i add my model to elastic.

标签: visual-studioasp.net-coreelasticsearchpostmankibana

解决方案


您正在设置configuration["elasticsearch:index"]为您的默认索引,connection setting但您正在创建 index machine,错误背后的原因是 index 的值configuration["elasticsearch:index"]不是 Elasticsearch 中的现有索引。


推荐阅读