3

I am trying to create an index using NEST 5.x pre release version for Elasticsearch 5.x. I have samples from 2.x which shows how index can be created using ElasticClient.CreateIndex method. Below is my sample code.

ESnode = new Uri("http://localhost:9200");
Nodesettings = new ConnectionSettings(ESnode);
Client = new ElasticClient(Nodesettings);

However, when I am typing below, there is NO autocomplete available.

Client.CreateIndex( c => c.

I am able to successfully get the health of the node using below code.

var res = Client.ClusterHealth();
Console.WriteLine("Status:" + res.Status);

I am having a complex document mapping for which I have defined the class structure and intend to use Automap method. Hence I am trying to create the index programatically to avoid manually creating the index.

I tried using some very old version of NEST (1.x) and I am able to get the autocomplete for createIndex. But both v2.4x and 5.x did not provide the autocomplete. Is there a new way to create index? Please let me know.

Thanks

1 Answer 1

11

You need to supply a name to the index, in addition to the delegate that provides additional index creation options

var createIndexResponse = client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfShards(1)
        .NumberOfReplicas(0)
    )
    .Mappings(m => m
        .Map<Conference>(d => d
            .AutoMap()
        )
    )
);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Russ. I was about to delete the question as I figured it by trial. I was able to create the index. But I had a second question. When I used the Automap, even nested properties also got mapped as simple properties. Also, second issue that I noticed was that all the properties were sorted by property name (when I tried the GET mapping). How do I correct these?
CLR types will be mapped as object field mappings by default when using .AutoMap() (except those in NEST that are known e.g. Attachment type). In order to map as nested types, you need to override the automapping with .Properties(): elastic.co/guide/en/elasticsearch/client/net-api/5.x/…. With regards to property names in json, they are never guaranteed to be in any particular order in json so order should not be relied upon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.