Search code examples
c#nestelasticsearch.net

Elasticsearch.NET version 7 - How to Create Index


In Elasticsearch.NET 6.x, it is possible create an index using IElasticClient method:

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

Method is removed in Elasticsearch.NET version 7.


Solution

  • In Elasticsearch.NET version 7 methods related to indices operations are moved into IndicesNamespace, so IndexExists method has been moved to:

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

    Also note, that Map(...) method is no longer nested inside of Mappings(...) method. Reason is that Elasticsearch server version 7 supports does not support multiple types per index (see Removal of mapping types), so one Map method per index is sufficient.

    Similarly, different methods have been moved to their own namespaces:

    • Cat
    • Cluster
    • Graph
    • Sql
    • Nodes
    • etc...