我正在使用自定义映射创建索引我的问题是与ElasticClient
的index,createindex等索引相对应的方法只获取索引名称作为输入参数,并从类的名称中识别类型名称作为泛型参数传递给它们有没有办法将类型名称传递给ElasticClient
方法,例如CreateIndex方法并强制它接受而不是使用类名???
这是我的代码的一部分
var qq = Elasticclient.CreateIndex("testindex", a => a.Mappings(f => f.Map<BankPaymentLogModel>(
b => b.Properties(c => c.String(d => d.Name(e => e.testProperty))
))));
任何帮助将不胜感激
答案 0 :(得分:1)
您可以使用几个选项来指定NEST将从POCO名称推断的不同类型名称
1.使用Map<T>(TypeName type, Func<TypeMappingDescriptor<T>, ITypeMapping>>)
重载
var createIndexResponse = client.CreateIndex("testindex", a => a
.Mappings(f => f
.Map<BankPaymentLogModel>("my-type", b => b
.Properties(c => c
.String(d => d
.Name(e => e.testProperty)
)
)
)
)
);
然而,使用此方法意味着您需要为使用.Type("my-type")
POCO的每个请求调用BankPaymentLogModel
,以便在请求中发送相同的类型名称。因此,以下选项可能更好
2.在ElasticsearchTypeAttribute
类型上使用BankPaymentLogModel
指定类型名称
[ElasticsearchType(Name = "my-type")]
public class BankPaymentLogModel
{
public string testProperty { get; set; }
}
var createIndexResponse = client.CreateIndex("testindex", a => a
.Mappings(f => f
.Map<BankPaymentLogModel>(b => b
.Properties(c => c
.String(d => d
.Name(e => e.testProperty)
)
)
)
)
);
3.如果您不喜欢属性,可以在ConnectionSettings
上为BankPaymentLogModel
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
.InferMappingFor<BankPaymentLogModel>(m => m
.TypeName("my-type")
);
var client = new ElasticClient(connectionSettings);
var createIndexResponse = client.CreateIndex("testindex", a => a
.Mappings(f => f
.Map<BankPaymentLogModel>(b => b
.Properties(c => c
.String(d => d
.Name(e => e.testProperty)
)
)
)
)
);
以上所有3个选项都会产生以下请求json
PUT http://localhost:9200/testindex
{
"mappings": {
"my-type": {
"properties": {
"testProperty": {
"type": "string"
}
}
}
}
}