backtrack
backtrack

Reputation: 8144

Elasticsearch - MapperParsingException[Malformed content, must start with an object]

I am trying to bulk index document into ES using BulkDescriptor in C#. i am using V1.7 ES. Following is my piece of code,

 public IBulkResponse IndexBulk(string index, string type, List<string> documents)
        {

                BulkDescriptor descriptor = new BulkDescriptor();
                foreach (var doc in documents)
                {
                    JObject data = JObject.Parse(documents); 

                    descriptor.Index<object>(i => i
                        .Index(index)
                        .Type(type)
                        .Id(data["Id"].toString())
                        .Document(doc));
                }
                return  _Client.Bulk(descriptor);

        }

But it is not inserting the documents, When i verified the response i saw the following message MapperParsingException[Malformed content, must start with an object]

Sample JSON document

{
"a" : "abc",
"b": { "c": ["1","2"]}
}

What went wrong in it?

Upvotes: 0

Views: 434

Answers (1)

Rob
Rob

Reputation: 9979

Issue here is passing raw json through strongly typed fluent bulk method.

What you are actually sending to elasticsearch is

{"index":{"_index":"test1","_type":"string"}}
"{"a" : "abc","b": { "c": ["1","2"]}}"

which is not correct.

Few ideas what you can do about this:

  1. use JObject to send correctly serialized object to elasticsearch

    descriptor.Index<JObject>(i => i
        .Index(index)
        .Type(type)
        .Id(data["Id"].toString())
        .Document(JObject.Parse(doc)));
    
  2. take advantage of using .Raw client to send raw json

    var json = new StringBuilder();
    json.AppendLine(@"{""index"":{""_index"":""indexName"",""_type"":""typeName""}}");
    json.AppendLine(@"{""a"" : ""abc"",""b"": { ""c"": [""1"",""2""]}}");
    
    _Client.Raw.Bulk(json2.ToString());
    

Hope it helps.

Upvotes: 2

Related Questions