我有一个ASP.NET WebForms Web应用程序,它使用ElasticSearch(使用NEST API)进行自动完成搜索,效果很好.但是,ElasticSearch中不时存储的文档结构(我只有一种类型的文档)发生了变化,并且映射需要随之改变.
我的方法是在C#代码中使用文档类型(和映射)的主要定义(只是在其属性上设置了相关ElasticProperty属性的C#类).我想能够询问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");
}
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 }))
);