NEST don't apply attribute mapping

Hi,
I use nest 7.0.0 and asp.net core 2.2. i want to create indxe from poco class. in elasticsearch index create but without any mapping. create index method is :

public async Task CreateIndex()
{
	try
	{
		var getIndexResponse =await ElasticClient.Indices.GetAsync("myindex");
		if (getIndexResponse.Indices == null || !getIndexResponse.Indices.Any())
		{
			var createIndexResponse = await ElasticClient.Indices.CreateAsync("myindex", c => c
				.Map(p => p.AutoMap<MyModel>()));
		}
	}
	catch (Exception)
	{
	}
}

and MyModel is like this:

[ElasticsearchType(IdProperty = nameof(Id), RelationName = "MyModelMessage")]
public class MyModel
{
	[Number(NumberType.Long, Index = true, DocValues = false)]
	public long UserId { get; set; }
	[Date(Index = true)]
	public DateTime CreatedAt { get; set; }
	[Text(Index = false)]
	public string ObjectName { get; set; }
	[Date(Index = true)]
	public DateTime UpdateAt { get; set; }
}

I checked this with NEST 7.0.1 against Elasticsearch 7.2.0 and I couldn't replicate the issue.

There's a few things that I would consider changing

  1. Change
var getIndexResponse =await ElasticClient.Indices.GetAsync("myindex");
if (getIndexResponse.Indices == null || !getIndexResponse.Indices.Any())
{
	var createIndexResponse = await ElasticClient.Indices.CreateAsync("myindex", c => c
		.Map(p => p.AutoMap<MyModel>()));
}

to use the Index exists API

var indexExistsResponse = await ElasticClient.Indices.ExistsAsync("myindex");
if (!indexExistsResponse.Exists)
{
	var createIndexResponse = await ElasticClient.Indices.CreateAsync("myindex", c => c
		.Map(p => p.AutoMap<MyModel>()));
}

It makes a HTTP HEAD request, so don't need to deserialize and check a response body

  1. Unless ThrowExceptions() is configured on ConnectionSettings, you likely don't need the try/catch block in this instance. Instead, you can check .IsValid on a response to determine if there was an issue with a request/response.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.