# .NET NEST ElasticClient LowLevel.IndexAsync Creating Document with Empty Fields

**URL:** <https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996>\
**Category:** Elasticsearch\
**Created:** [March 28, 2018, 9:32pm UTC](https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996 "2018-03-28T21:32:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeStone](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CodeStone](https://discuss.elastic.co/u/CodeStone)\
**Post date:** [March 28, 2018, 9:32pm UTC](https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996/1 "2018-03-28T21:32:49Z")

</div>

Below is a portion of my code, that I've taken from [here](https://github.com/Azure/azure-diagnostics-tools/tree/master/AzureFunctions/EventHubsToElastic/). I had to make few changes to make it work with the new v6.X of Elasticsearch .NET libraries. I am running this code inside an Azure Function.

It runs without any errors and creates new documents BUT with empty field values. If I take the same JSON payload and PUT in Elasticsearch using Postman, the document gets indexed fine, with all fields populated. Please let me know if I am using the right Elasticsearch API methods, and using them right.

```
string strJsonMessage = @"
{ 
    message : {
        content: 'Test Message Content'
    }
}";

ConnectionSettings connectionSettings = new ConnectionSettings(new Uri("xxx")).BasicAuthentication("xx", "xx");
ElasticClient client = new ElasticClient(connectionSettings);

JObject msg = JObject.Parse(strJsonMessage);
var result = await client.LowLevel.IndexAsync<BytesResponse>("events-2018.03.27", "event", PostData.Serializable(msg));

if (result.Success)
{
    log.Info("Data successfully sent.");
	log.Verbose(result.Body);
}
else
{
	log.Error("Failed to send data.");
}

****OUTPUT TRACE:****

2018-03-27T18:37:18.961 [Info] Data successfully sent.
2018-03-27T18:37:18.961 [Verbose] {"_index":"events-2018.03.27","_type":"event","_id":"u9HPaGIBBm3ZG7GB5jM_","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":29,"_primary_term":1}

```

Querying elasticsearch for this document by its ID gives me this:

```
{
    "_index": "events-2018.03.27",
    "_type": "event",
    "_id": "u9HPaGIBBm3ZG7GB5jM_",
    "_version": 1,
    "found": true,
    "_source": {
        "message": {
            "content": []
        }
    }
}
```

---

<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:** [March 29, 2018, 11:47am UTC](https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996/2 "2018-03-29T11:47:04Z")

</div>

NEST 6.x ships with an IL merged and renamespaced version of [Json.Net](http://Json.Net), which means ultimately means that it doesn't know how to serialize `Newtonsoft.Json.Linq.JObject` types in any special way, unlike using [Json.NET](http://Json.NET) directly.

[You can hook up NEST to use Json.Net as the serializer to handle this](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html#_jsonnetserializer) by installing the [NEST.JsonNetSerializer](https://www.nuget.org/packages/NEST.JsonNetSerializer/) nuget package and initializing `ConnectionSettings` as follows

```auto
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
    new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);

```

Then `JObject` will be serialized as expected.

You could avoid the deserialization then reserialization altogether however by passing _valid_ JSON directly to the low level client call.

```auto
string json = @"{ 
    ""message"" : {
        ""content"": ""Test Message Content""
    }
}";

var result = client.LowLevel.Index<BytesResponse>("events-2018.03.27", "event", json);

```

In doing so, you don't need to take the dependency on NEST.JsonNetSerializer nuget package either.

`json` is implicitly converted to `PostData`. You can use `PostData.String(json);` if you'd like to be explicit.

Additionally, if you don't need to response body, you can return `VoidResponse`.

---

<div class="post-metadata">

**Author:** ![CodeStone](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CodeStone](https://discuss.elastic.co/u/CodeStone)\
**Post date:** [March 29, 2018, 11:20pm UTC](https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996/3 "2018-03-29T23:20:08Z")

</div>

Thank you very much!! That resolved my issue. I read that [json.net](http://json.net) has been merged into the latest NEST, couldn't find any samples to properly send JSON in string. Thanks again!!

---

<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:** [April 26, 2018, 11:20pm UTC](https://discuss.elastic.co/t/net-nest-elasticclient-lowlevel-indexasync-creating-document-with-empty-fields/125996/4 "2018-04-26T23:20:25Z")

</div>

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