Mike Barnes
Mike Barnes

Reputation: 4305

Cannot index document

I have written some code using the Elasticsearch.Net & NEST client library that should index a document without using a POCO for mapping fields as I have many different documents.

Question 1) Is this correct way to do create an index, does the .AddMapping<string>(mapping => mapping.Dynamic(true)) create the mapping based on the document passed in?

var newIndex = client.CreateIndex(indexName, index => index
    .NumberOfReplicas(replicas)
    .NumberOfShards(shards)
    .Settings(settings => settings
    .Add("merge.policy.merge_factor", "10")
    .Add("search.slowlog.threshold.fetch.warn", "1s")
   )
    .AddMapping<string>(mapping => mapping.Dynamic(true))
);

Question 2) Is this possible?

string document = "{\"name\": \"Mike\"}";
var newIndex = client.Index(document, indexSelector => indexSelector
        .Index(indexName)
 );

When I run code in "Question 2" it returns: {"Unable to perform request: 'POST ' on any of the nodes after retrying 0 times."}

Upvotes: 0

Views: 845

Answers (1)

Martijn Laarman
Martijn Laarman

Reputation: 13536

NEST only deals with typed objects in this case passing a string will cause it to index the document into /{indexName}/string/{id}.

Since it can't infer an id from string and you do not pass it one it will fail on that or on the fact that it can't serialize a string. I'll update the client to throw a better exception in this case.

If you want to index a document as string use the exposed Elasticsearch.NET client like so:

client.Raw.Index(indexName, typeName, id, stringJson);

If you want elasticsearch to come up with an id you can use

client.Raw.Index(indexName, type, stringJson);

client is the NESTclient and the Raw property is an Elasticsearch.Net client with the same connectionsettings.

Please note that I might rename Raw with LowLevel in the next beta update, still debating that.

Upvotes: 2

Related Questions