Antony Francis
Antony Francis

Reputation: 304

Sharing index mapping configuration in NEST 2.3.3?

Upgrading Elastic & NEST search from 1.6.2 to 2.3.3.

We used be able to share the same PutMappingDescriptor between ElasticClient.CreateIndex() and ElasticClient.Map().

But in 2.3.3, the CreateIndex needs TypeMappingDescriptor and Map requires PutMappingDescriptor.

How do we share the same mapping configuration?

Upvotes: 2

Views: 263

Answers (1)

foresightyj
foresightyj

Reputation: 2106

The official Nest developers answered this question in their github, linked here.

Basically, you don't use Func<PutMappingDescriptor<Project>, IPutMappingRequest> but PutMappingDescriptor<Project> directly. by newing up a PutMappingDescriptor<Project> and build up your fluent mapping from there.

Creating index expects ITypeMapping while updating index expects IPutMappingRequest which implements ITypeMapping. So you can satisfy both by using PutMappingDescriptor.

To create an index, use:

``` client.CreateIndex("projects", c => c .Mappings(ms => ms .Map(m => GetMapping()) ) );

``` where you ignore m passed in in the lambda and use the one you created. The reason why you can do that can be found in NEST's source code where it creates an empty TypeMappingDescriptor for your to further build upon:

public MappingsDescriptor Map<T>(Func<TypeMappingDescriptor<T>, ITypeMapping> selector) where T : class => Assign(typeof (T), selector?.Invoke(new TypeMappingDescriptor<T>()));

To update mapping, do:

client.Map(GetMapping());

Upvotes: 1

Related Questions