Search code examples
elasticsearchmappingnest

Set custom type name in mapping


I need to create a document mapping that has a custom name. Currently I have the following mapping for my document on the CreateIndexDescriptor object:

.Mappings(m => m
  .Map<MyDocType>(mDetails => mDetails.AutoMap()));

Which creates a document mapping called mydoctype. How can I modify this so it creates a document whose type name is my_doctype?


Solution

  • In NEST 7.x, this is not possible - the document type will be _doc, in line with the roadmap for the removal of mapping types.

    In NEST 6.x, you can specify the type name to use in a few different ways:

    1. Using ElasticsearchTypeAttribute on the POCO

      [ElasticsearchType(Name = "my_doctype")]
      public class MyDocType{ }
      
    2. Using DataContractAttribute on the POCO

      [DataContract(Name = "my_doctype")]
      public class MyDocType{ }
      
    3. Using .DefaultMappingFor<T>() on ConnectionSettings

      var settings = new ConnectionSettings()
          .DefaultMappingFor<MyDocType>(m => m
              .IndexName("my_doc_type_default_index")
              .TypeName("my_doctype")
          );
      
      var client = new ElasticClient(settings);