带有NEST的索引JsonObject具有空值

时间:2018-05-18 07:49:10

标签: c# elasticsearch nest

我希望使用NEST索引JsonObjects,在将属性发布到索引中但值为空“[]”之后。当我用Postman发布相同的json时,结果是正确的。

指数:

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

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

搜索查询:

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

搜索结果

{
    "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": []
                    }
                }
            },

1 个答案:

答案 0 :(得分:1)

如果你使用JObject作为文档类型,或者你的文档包含JObject,你还需要引用NEST.JsonNetSerializer nuget包并连接JsonNetSerializer,如下所示

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

var client = new ElasticClient(connectionSettings); 这是必需的,因为NEST 6.x通过IL合并,内化和重新命名空间来消除对Json.NET的直接依赖。这带来的变化之一是,现在,NEST不知道如何专门处理Newtonsoft.Json.Linq.JObject,因此需要特别知道如何处理该类型的NEST.JsonNetSerializer依赖。

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