elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

比较ElasticSearch服务器上的映射与C#类中的推断映射?

比较ElasticSearch服务器上的映射与C#类中的推断映射?,c#,elasticsearch,nest,C#,elasticsearch,Nest,我有一个ASP.NET WebForms web应用程序,它使用ElasticSearch(使用NEST API)进行自动完成搜索,效果非常好。但是,ElasticSearch中存储的文档结构(我只有一种文档类型)会不时发生变化,映射也需要随之变化 我的方法是在C#code中对文档类型(和映射)进行主定义(只是一个C#class,在其属性上设置了相关的ElasticProperty属性)。我希望能够询问NEST ElasticSearch服务器的映射定义是否与可以从我的文档类推断的映射定义匹配,

我有一个ASP.NET WebForms web应用程序,它使用ElasticSearch(使用NEST API)进行自动完成搜索,效果非常好。但是,ElasticSearch中存储的文档结构(我只有一种文档类型)会不时发生变化,映射也需要随之变化

我的方法是在C#code中对文档类型(和映射)进行主定义(只是一个C#class,在其属性上设置了相关的
ElasticProperty
属性)。我希望能够询问NEST ElasticSearch服务器的映射定义是否与可以从我的文档类推断的映射定义匹配,如果不匹配,则更新服务器的映射。比如:

ElasticClient client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")), "my_index");
// Hypothetical code below - does NEST offen an API which lets me do this if statement?
if (!client.GetMapping("MyDocument").Matches<MyDocument>()) {
    client.CloseIndex("my_index"); // Is this necessary when updating mapping?
    client.Map<MyDocument>(m => m.MapFromAttributes());
    client.OpenIndex("my_index");
}
ElasticClient=newelasticclient(新连接设置(新Urihttp://localhost:9200),“我的指数”);
//下面假设的代码-NEST是否提供了一个API,让我执行这个if语句?
如果(!client.GetMapping(“MyDocument”).Matches()匹配){
client.CloseIndex(“my_index”);//更新映射时是否需要这样做?
client.Map(m=>m.MapFromAttributes());
client.OpenIndex(“我的索引”);
}

NEST提供这样的API吗?

可以这样做,而无需在集群中创建任何内容:

var getIndexResponse = await _elasticClient.GetIndexAsync(indexName);
IIndexState remote = getIndexResponse.Indices[indexName];
// move the index definition out of here and use it to create the index as well
IIndexState local = new CreateIndexDescriptor(indexName);
// only care about mappings
var areMappingsSynced = JToken.DeepEquals
(
    JObject.Parse(_elasticClient.Serializer.SerializeToString(new { local.Mappings })),
    JObject.Parse(_elasticClient.Serializer.SerializeToString(new { remote.Mappings }))
);

据报道,目前还没有这样的事情。我也非常需要这个功能。我制定了一个黑客解决方案,使用当前映射创建一个临时索引,然后从临时索引和实际索引中获取映射,并区分返回的json。它对我来说工作得很好。最新版本的ElasticClient使用RequestResponseSerializer而不是Serializer