NEST: Specify type name on mapping

Hi guys,

This code:

client.CreateIndex(indexName,
      s => s.Mappings(ms => ms
       .Map<NestedMessage>(m => m.AutoMap())));

will generate mapping for type with name "nestedMessage":

{"mappings":{"nestedMessage":{"properties":{ .. }}}}

Is there any way to rename it?

Thanks

Yes, you can name the type in Elasticsearch differently to your POCO a few different ways

1.Using the ElasticsearchType attribute applied to NestedMessage and specifying the Name property

[ElasticsearchType(Name="message")]
public class NestedMessage
{
}

2.Using InferMappingFor<T>() on ConnectionSettings

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
    .InferMappingFor<NestedMessage>(m => m
        .TypeName("message")
    );

var client = new ElasticClient(settings);

client.CreateIndex("index-name",  s => s
    .Mappings(ms => ms
        .Map<NestedMessage>(m => m
            .AutoMap()
        )
    )
);

3.Using .MapDefaultTypeNames() on ConnectionSettings (although this will be removed in future, InferMappingFor<T>() is the way to go. Listed here for completeness)

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
    .MapDefaultTypeNames(d => d
        .Add(typeof(NestedMessage), "message")
    );

var client = new ElasticClient(settings);

client.CreateIndex("index-name",  s => s
    .Mappings(ms => ms
        .Map<NestedMessage>(m => m
            .AutoMap()
        )
    )
);

All will yield

{
  "mappings": {
    "message": {
      "properties": {}
    }
  }
}

Hi,

Perfect, thanks