Search code examples
c#elasticsearchnest

Index JsonObject with NEST has empty values


I want to index JsonObjects with NEST, after posting the properties are in the index but the values are empty "[]". When I post the same json with Postman the result is correct.

Index:

string indexName = "testindex";
        IIndexResponse response = client.Index<JObject>(docItem, i => i.Type("my_type").Index(indexName));

json in docItem:

{
    "Source":"test",
    "CreatedAt": "2018-05-26 12:23:33",
    "SessionId":"1234",
    "ResponseParam":{
        "ItemA":"bla",
        "ItemB": 123
    }
}

search query:

http://[IP]:9200/testindex/_search

search result

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "testindex",
                "_type": "my_type",
                "_id": "u44ucmMB687Uyj7O8xKY",
                "_score": 1,
                "_source": {
                    "Source": [],
                    "CreatedAt": [],
                    "SessionId": [],
                    "ResponseParam": {
                        "ItemA": [],
                        "ItemB": []
                    }
                }
            },

Solution

  • If you're using JObject as the document type, or your document contains JObject, you will need to also reference the NEST.JsonNetSerializer nuget package and hook up the JsonNetSerializer as follows

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

    var client = new ElasticClient(connectionSettings); This is required because NEST 6.x removed the direct dependency on Json.NET by IL-merging, internalizing and re-namespacing it. One of the changes this brings about is that now, NEST does not know how to specially handle Newtonsoft.Json.Linq.JObject, so the dependency on NEST.JsonNetSerializer which does know how to handle that type specially, is needed.

    Source: https://discuss.elastic.co/t/elasticsearch-net-nest-issue-with-api-after-upgrade-to-6-2-3/127690