【问题标题】:How can I bulk insert with Elastic low-level-client for nested typed object?如何使用 Elastic 低级客户端批量插入嵌套类型对象?
【发布时间】:2019-11-21 13:31:16
【问题描述】:

我试图插入具有嵌套数据类型的整个数据,但低级客户端(c#)无法插入。

最新的 elasticsearch 文档没有解释我们如何做到这一点。

所以,我不确定我需要做什么。有人可以帮我吗?

VendorProduct 被创建为嵌套类型。

映射:

"properties": {

            "vendorProducts": {
                "properties": {
                    ...
                },
                "type": "nested"
            },

        }

批量 API 上的批量插入请求;

{"index":{"_index":"productindextemp-local82","_type":"elasticproduct","_id":"1715"}}
{
    "id": 1715,
    "productTypeId": 5,
...

    "vendorProducts": [
        {
            "id": 124550,
            "productId": 1715,
            "vendorId": 8,
..
        },
        {
            "id": 451542,
            "productId": 1715,
            "vendorId": 15,
            ..
        }
    ]
}

服务器: AWS 弹性搜索服务 v7.1

c# 参考: Elasticsearch.Net v7.4.1 巢v7.4.1

我们的 C# 端代码是 ;

var settings = new ConnectionSettings(new System.Uri(ElasticUrl));
settings.DefaultMappingFor<ElasticProduct>(d => d.IndexName(newTempIndexName));
settings.RequestTimeout(TimeSpan.FromHours(5));
var client = new ElasticClient(settings);




if (!client.Indices.Exists(newTempIndexName).Exists)
{
    client.Indices.Create(
        newTempIndexName,
        c => c.Map<ElasticProduct>(m => m
            .AutoMap()
            .Properties(p => p
                .Nested<List<ElasticVendorProduct>>(n => n
                    .Name(nn => nn.VendorProducts)
                    .AutoMap()
                )
             )
        )
    );
}



StringBuilder stringBuilder = new StringBuilder();
foreach (var data in productBulkDataItems)
    stringBuilder.Append(data).Append("\n");
var result = client.LowLevel.Bulk<BulkResponse>(newTempIndexName, stringBuilder.ToString());

我们在发送请求后收到此错误。因此,结果为空。我无法处理这个问题。我们已经从 .net 上的 Nest 客户端提取了 Json 请求,然后我们尝试在 kibana 上手动发送请求。这种方法也给了我们同样的结果。

例外:

 {index returned 400 _index: productindextemp-local82 _type: elasticproduct _id: 1715 _version: 0 error: Type: illegal_argument_exception Reason: "object mapping [vendorProducts] can't be changed from nested to non-nested"}

【问题讨论】:

  • 请您编辑您的问题以显示您正在做什么以获得您看到的异常
  • 什么是productBulkDataItemsElasticProductElasticVendorProduct 的定义是什么?

标签: c# elasticsearch nested nest bulk


【解决方案1】:

当您尝试创建索引时,您必须将索引名称从“elasticproduct”更改为“_doc”。您的输出数据应如下所示:

    "mappings": {
        "_doc": {
            "properties": {
            ...
            }
        }
    }

尝试使用"_type":"_doc" 而不是"_type":"elasticproduct"

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2013-07-23
    • 2017-09-06
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多