首页 > 解决方案 > 带有 NEST 的索引 JsonObject 具有空值

问题描述

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

指数:

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

docItem中的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": []
                    }
                }
            },

标签: c#elasticsearchnest

解决方案


如果您使用 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


推荐阅读