Search code examples
elasticsearchnest

Bulk update with mapping in NEST


I'm creating an index with mappings the following way:

_elasticClient.Indices.Create(name, c => c.Map(/* omitted for brevity */));

_elasticClient.BulkAll(data, b => b
                .Index(name)
                .BackOffTime("30s")
                .BackOffRetries(2)
                .RefreshOnCompleted()
                .MaxDegreeOfParallelism(Environment.ProcessorCount)
                .Size(50)).Wait(TimeSpan.FromMinutes(15), _ => { });

_elasticClient.Indices.PutAlias(name, Alias);

I'm just trying to understand how the bulk update works and make sure I do this correctly. Even if I remove _elasticClient.Indices.Create, an index still seems to be created. Does a POST to index_v1/_bulk create the index if it doesn't exist, but update it with data if I create it first with my first line?


Solution

  • By default, if data is indexed into an index that does not yet exist, Elasticsearch will create the index and infer the mapping for documents based on the first document it sees. This can be useful, but typically for a search use case, you want to explicitly control the mapping for documents to apply specific analyzers, etc., so creating the index with the mapping you desire is the preferred approach.