# Bulk insert issue with ElasticSearch.Net 6.0.2

**URL:** <https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284>\
**Category:** Elasticsearch\
**Created:** [April 17, 2018, 5:27am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284 "2018-04-17T05:27:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [April 17, 2018, 5:27am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/1 "2018-04-17T05:27:58Z")

</div>

Hi

Following code in NEST with lowlevelclient

```auto
ElasticLowLevelClient objClient = new ElasticLowLevelClient(objSettings);

    string strData = "{\"RollNo\": \"1\",\"Name\": \"Abc\"}";
    JObject data =JObject.Parse(strData);

       
      var objIndex = 
                                        new
                                        {
                                            index =
                                            new
                                            {
                                                _index = "strIndex",
                                                _type = "strType",
                                                _id = "strId"
                                            }
                                        };
         lstRowsToCache.Add(objIndex ); //Adding to elastic

         lstRowsToCache.Add(data); //Not adding to elastic

        indexResponse = objClient.Bulk<Stream>(lstRowsToCache);

```

- Data in AnonymousTypes are accepting but not JObject

Thanks  
Aneesh L

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 18, 2018, 2:38am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/2 "2018-04-18T02:38:20Z")

</div>

Could you please elaborate on what the issue is?

---

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [April 18, 2018, 3:26am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/3 "2018-04-18T03:26:26Z")

</div>

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test1",
        "_type": "test1",
        "_id": "1",
        "_score": 1,
        "_source": {
          "RollNo": [],
          "Name": []
        }
      }
    ]
  }
}

```

Data is saved in elastic like this.  
Instead of values, empty arrays added to elastic.  
Is there any problem with my parsing?  
How we can add string to elastic instead of JSON?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 18, 2018, 4:11am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/4 "2018-04-18T04:11:10Z")

</div>

There's a couple things incorrect:

1. `objClient.Bulk<Stream>(lstRowsToCache);` can't be using `Elasticsearch.Net 6.0.2` because this version of the client does not support returning `Stream` from the bulk request. With the low level client, this should be one of `VoidResponse`, `StringResponse` or `BytesResponse`. It looks like you might be using a `Elasticsearch.Net 5.x` version
2. The default serializer within `Elasticsearch.Net` does not know how to serialize a [Json.NET](http://Json.NET) `JObject` to JSON because it has no dependency on [Json.NET](http://Json.NET).

A minimum working example in 6.0.2 would be

```auto
var client = new ElasticLowLevelClient();
var data = new { RollNo = "1", Name = "Abc"};
var lstRowsToCache = new List<object>();

var objIndex = new
{
  index =
  new
  {
      _index = "strIndex",
      _type = "strType",
      _id = "strId"
  }
};

lstRowsToCache.Add(objIndex);
lstRowsToCache.Add(data); 

var indexResponse = client.Bulk<VoidResponse>(PostData.MultiJson(lstRowsToCache));

```

which uses an anonymous type for the document. This sends the following request to Elasticsearch

```json
POST http://localhost:9200/_bulk
{"index":{"_index":"strIndex","_type":"strType","_id":"strId"}}
{"RollNo":"1","Name":"Abc"}

```

If you want to use `JObject`, you'll need to hook in a serializer that understands `JObject` e.g. implement an `IElasticsearchSerializer` that uses [Json.NET](http://Json.NET) for serialization. A naive implementation may look like

```auto
private static void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionConfiguration(pool, new JsonNetSerializer());
    var client = new ElasticLowLevelClient(settings);

    string strData = "{\"RollNo\": \"1\",\"Name\": \"Abc\"}";
    JObject data = JObject.Parse(strData);
    var lstRowsToCache = new List<object>();

    var objIndex = new
    {
      index =
      new
      {
          _index = "strIndex",
          _type = "strType",
          _id = "strId"
      }
    };
    
    lstRowsToCache.Add(objIndex);
    lstRowsToCache.Add(data);

    var indexResponse = client.Bulk<VoidResponse>(PostData.MultiJson(lstRowsToCache));
}

public class JsonNetSerializer : IElasticsearchSerializer
{
    private static JsonSerializer serializer = JsonSerializer.CreateDefault();
    private static JsonSerializer indentedSerializer = 
        JsonSerializer.CreateDefault(new JsonSerializerSettings
        {
            Formatting = Newtonsoft.Json.Formatting.Indented
        });
    
    public object Deserialize(Type type, Stream stream)
    {
        var streamReader = new StreamReader(stream);
        var reader = new JsonTextReader(streamReader);
        return serializer.Deserialize(reader, type);
    }

    public T Deserialize<T>(Stream stream)
    {
        return (T)Deserialize(typeof(T), stream);
    }

    public Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default(CancellationToken))
    {
        var o = Deserialize(type, stream);
        return Task.FromResult(o);
    }

    public Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
    {
        var o = Deserialize<T>(stream);
        return Task.FromResult(o);
    }

    public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented)
    {
        var writer = new StreamWriter(stream);
        if (formatting == SerializationFormatting.Indented)
            indentedSerializer.Serialize(writer, data, typeof(T));
        else
            serializer.Serialize(writer, data, typeof(T));    
            
        writer.Flush();
    }

    public Task SerializeAsync<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.Indented, CancellationToken cancellationToken = default(CancellationToken))
    {
        Serialize(data, stream, formatting);
        return Task.CompletedTask;
    }
}

```

**NOTE:** the implementation does not wrap `IDisposable` types in `using` statements because the client library will close the stream.

---

<div class="post-metadata">

**Author:** ![aneesh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aneesh/32/37819_2.png) [@aneesh](https://discuss.elastic.co/u/aneesh)\
**Post date:** [April 18, 2018, 7:18am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/5 "2018-04-18T07:18:58Z")

</div>

Example with anonymous type, we can't use because columns are dynamic  
We need to use JObject, But for Deserialize(Stream stream), what type need to send.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 18, 2018, 9:39am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/6 "2018-04-18T09:39:10Z")

</div>

> [@aneesh](#):
>
> We need to use JObject, But for Deserialize(Stream stream), what type need to send.

Take a look at the last example I provided, which uses `JObject` and configures an `IElasticsearchSerializer` that uses Newtonsoft.Json

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 16, 2018, 9:39am UTC](https://discuss.elastic.co/t/bulk-insert-issue-with-elasticsearch-net-6-0-2/128284/7 "2018-05-16T09:39:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
