“Clone” index mappings

2019-08-27 02:05发布

问题:

I have an index which I will be reindexing. At the moment I want to create a new index, which should contain the exact same mappings that can be found in the original index.

I've got this:

var srcMappings = client.GetMapping(new GetMappingRequest((Indices)sourceIndexName)).Mappings;

And I try to create an index:

var response = client.CreateIndex(destinationIndex, c => c
    .Settings(...my settings ...)
    .Mappings(... what here? ...)
);

What exactly should I pass to the .Mappings(...) above so that the mappings from the source index are replicated into the target index? I don't want to explicitly 'know' about the types.

I am trying to use Nest.

Alternatively, is there a Reindex API which would take the destination index name and create the index for me, together with the mappings of the source?

回答1:

You can get the mappings from one index and use them to create the mappings in another index with

var client = new ElasticClient();

var getIndexResponse = client.GetIndex("assignments");

var createIndexResponse = client.CreateIndex("assignments2", c => c
    .Mappings(m => Promise.Create(getIndexResponse.Indices["assignments"].Mappings))
);

You'll need an IPromise<T> implementation to do so

public class Promise
{
    public static IPromise<TValue> Create<TValue>(TValue value) where TValue : class => 
        new Promise<TValue>(value);
}

public class Promise<T> : IPromise<T> where T : class
{
    public T Value { get; } 
    public Promise(T value) => Value = value;
}

The Promise is needed in some places in NEST's fluent API implementation where values are additive and a final value needs to be returned at a later point.

You can also do the same using the object initializer syntax and no Promise<T>

var createIndexResponse = client.CreateIndex(new CreateIndexRequest("assignments2")
{
    Mappings = getIndexResponse.Indices["assignments"].Mappings
});

Alternatively, is there a Reindex API which would take the destination index name and create the index for me, together with the mappings of the source?

There are two Reindex APIs within NEST; an Observable implementation that has been around since NEST 1.x, and the Reindex API as available within Elasticsearch since 2.3 (known as ReindexOnServer in NEST). The former Observable implementation can create the destination index for you, although it will copy all settings, mappings and aliases. The latter Reindex API does not create the destination index as part of the operation, so it needs to be set up before starting the reindex process.