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
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
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.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.