包含空值

时间:2018-02-22 14:09:26

标签: elasticsearch nest

我们最近升级到弹性搜索v5和嵌套v5.6

我们尝试将字段设置为null,但是,默认序列化设置忽略空值。

var pool = new SingleNodeConnectionPool(new Uri("http://local:9200"));

var connectionSettings =
    new ConnectionSettings(pool)
    .DisableDirectStreaming();

var elasticClient = new ElasticClient(connectionSettings);

var indexName = "myIndexName";
var typeName = "myTypeName";
var documentId = 2;

var pendingDescriptor = new BulkDescriptor();
pendingDescriptor.Index(indexName).Type(typeName);

var pendingUpdate = new Dictionary<string, object>
            {
                { $"DocumentType_TAG_PENDING_Id", null }
            };

var updateRequest = new UpdateRequest<dynamic, dynamic>(indexName, typeName, new Id(documentId));
updateRequest.Doc = pendingUpdate;
elasticClient.Update<dynamic>(updateRequest);

这导致请求:

{"update":{"_id":2,"_retry_on_conflict":3}}
{"doc":{}}

字段值未设置为空

我在阅读https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/modifying-default-serializer.html

之后尝试修改序列化程序以包含空值
var connectionSettings =
    new ConnectionSettings(pool, connection, new SerializerFactory((settings, values) =>
    {
        settings.NullValueHandling = NullValueHandling.Include;
    }));

现在我的请求变为:

{"update":{"_index":null,"_type":null,"_id":2,"_version":null,"_version_type":null,"_routing":null,"_parent":null,"_timestamp":null,"_ttl":null,"_retry_on_conflict":3}}
{"doc":{"DocumentType_TAG_PENDING_Id":null},"upsert":null,"doc_as_upsert":null,"script":null,"scripted_upsert":null}

我收到以下错误:

{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Current token (VALUE_NULL) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@181f5854; line: 1, column: 82]"}],"type":"json_parse_exception","reason":"Current token (VALUE_NULL) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@181f5854; line: 1, column: 82]"},"status":500}

请帮忙

1 个答案:

答案 0 :(得分:1)

到目前为止,我们有两个选择:

  1. 升级到v6,它们分隔了文档和请求序列化程序。 因此,我们可以自定义文档序列化的方式,而不会影响请求/响应标头。有关详细信息,请参阅https://www.elastic.co/guide/en/elasticsearch/client/net-api/master/custom-serialization.html

  2. 使用具有发布请求的弹性搜索低级客户端,避免使用嵌套序列化程序。 https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/elasticsearch-net.html

  3. 如果一切正常,我们首选的方法是升级,如果出现任何问题,请恢复为低级客户端