Elasticsearch.NET 版本 7 - 如何创建索引

Elasticsearch.NET version 7 - How to Create Index

在Elasticsearch.NET6.x中,可以使用IElasticClient方法创建索引:

var response = elasticClient.Create(
                    "my-index-name",
                    index =>  index .Mappings(
                        ms => ms.Map<MyDocumentType>(
                            x => x.AutoMap()
                        )
                    )
                );

方法已在 Elasticsearch.NET 版本 7 中删除。

在 Elasticsearch.NET 版本 7 中,与索引操作相关的方法已移至 IndicesNamespace,因此 IndexExists 方法已移至:

var response = elasticClient.Indices.Create(IndexName,
                    index => index.Map<ElasticsearchDocument>(
                        x => x.AutoMap()
                    ));

另请注意,Map(...) 方法不再嵌套在 Mappings(...) 方法中。原因是 Elasticsearch 服务器版本 7 支持不支持每个索引的多种类型(参见 Removal of mapping types),因此每个索引一个 Map 方法就足够了。

同样,不同的方法已移至它们自己的命名空间:

  • 集群
  • 图表
  • Sql
  • 节点
  • 等...