FirstDivision FirstDivision - 1 month ago 32
C# Question

CreateIndex in Nest / Elastic Search 2.x after upgrade from 1.x

I upgraded my Nest and Elastic Search NuGet packages to 2.4.6 from 1.7.1. Everything seems to be working ok, but one thing I'm wary of is where I create the index.

There were two settings that I used to set in the old 1.x world that I couldn't figure out how to set in the 2.x world. Creating an index went from this:

ixSettings.NumberOfReplicas = 1;
ixSettings.NumberOfShards = 5;
ixSettings.Settings.Add("merge.policy.merge_factor", "10");
ixSettings.Settings.Add("search.slowlog.threshold.fetch.warn", "1s");

EsClient.CreateIndex(c => c
.Index(ES_Index)
.InitializeUsing(ixSettings)
);


to this:

EsClient.CreateIndex(ES_Index, c => c
.Settings(s => s
.NumberOfReplicas(1)
.NumberOfShards(5)));

var mapResponse = EsClient.Map<WebPage>(m => m.AutoMap()); //apply the index mapping


I think the merge policy and fetch warn settings were something I got from an Elastic Search "Getting Started Guide". The "merge_factor" defaults to 10, so that doesn't matter, and I guess the "search.slowlog.threshold.fetch.warn" is a logging thing.

My questions are:


  1. Am I ok leaving those off my index creation in my new code?

  2. How do I set them if I want to?


Answer

The settings upon index creation have been improved to provide a fluent API and Object Initializer syntax for the settings that are allowed at index creation time.

You can set any key/value with .Setting()

client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfReplicas(1)
        .NumberOfShards(5)
        .Setting("merge.policy.merge_factor", "10")
        .Setting("search.slowlog.threshold.fetch.warn", "1s")
    )
);

But it should be noted that merge.policy.merge_factor is removed from Elasticsearch 2.0+. You can find the settings that are supported on

client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfReplicas(1)
        .NumberOfShards(5)
        .Merge(m => m
            .Policy(mp => mp
                . // See which settings are available here
            )
        )
    )
);

Similarly, search.slowlog.threshold.fetch.warn can be set with

client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfReplicas(1)
        .NumberOfShards(5)
        .SlowLog(sl => sl
            .Search(ssl => ssl
                .Fetch(f => f
                    .ThresholdWarn("1s")
                )
            )
        )
    )
);

which produces the following request:

PUT http://localhost:9200/index-name 
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.search.slowlog.threshold.fetch.warn": "1s",
    "index.number_of_shards": 5
  }
}