2

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?

hyankov
  • 4,049
  • 1
  • 29
  • 46

1 Answers1

2

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.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • Thank you. The Observable implementation of Reindex basically creates a full clone of an index? You are referring to `ReindexObserver` and .`Reindex`, right? Can I override some of the settings, for example number of shards of the index? And to see if I get this right, the NEST 1.x version does reindexing on the client, while the ES 2.3 Reindex is accessed via `ReindexOnServer` and is done on the server? – hyankov Mar 22 '18 at 13:27
  • 1
    Correct, Observable `Reindex`, creates a clone of an index, although you can control which documents are reindexed, and re-map how they are indexed.You can control settings, but only outside of the `Reindex` call, then pass them in, similar to creating an index in the answer. `Reindex` pulls documents to the application side using `scroll` API and sends index requests to Elasticsearch; `ReindexOnServer` performs reindexing within Elasticsearch. Both exist in NEST 2.x+ (`ReindexOnServer` needs to run against Elasticsearch 2.3.0+). – Russ Cam Mar 22 '18 at 22:23