【问题标题】:Compare mapping on ElasticSearch server with inferred mapping from C# class?将 ElasticSearch 服务器上的映射与 C# 类的推断映射进行比较?
【发布时间】:2018-03-15 04:42:56
【问题描述】:

我有一个 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?

【问题讨论】:

  • 根据github.com/elastic/elasticsearch-net/issues/1065,目前还没有这样的事情。我也非常需要这个功能。我制定了一个 hacky 解决方案,它使用当前映射创建一个临时索引,然后从临时索引和实际索引中获取映射,并对返回的 json 进行比较。对我来说效果很好。

标签: c# elasticsearch nest


【解决方案1】:

无需在集群中创建任何东西就可以这样做:

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 }))
);

【讨论】:

  • 最新版本的ElasticClient使用RequestResponseSerializer而不是Serializer
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
相关资源
最近更新 更多