【问题标题】:Specify the _id field using Bulk.IndexMany in ElasticSearch在 ElasticSearch 中使用 Bulk.IndexMany 指定 _id 字段
【发布时间】:2017-12-31 01:40:32
【问题描述】:

我在使用批量 API (C# NEST v5.4) 插入文档时遇到问题。我有一个文档数组,在数组里面我有我的 ID。

我的代码是:

documents = documents .ToArray();

Client.Bulk(bd =>
bd.IndexMany(documents,
    (descriptor, s) => descriptor.Index(indexName)));

如何使用描述符手动插入 _id

提前致谢!

【问题讨论】:

    标签: c# elasticsearch nest elasticsearch-bulk-api


    【解决方案1】:

    您可以像在BulkDescriptor 上设置索引名称一样设置_id。鉴于以下 POCO

    public class Message
    {
        public string Content { get; set; }
    }
    

    例如使用递增计数器设置 id

    var documents = new[] {
        new Message { Content = "message 1" },
        new Message { Content = "another message" },
        new Message { Content = "yet another one" }
    };
    
    var indexName = "index-name";   
    var id = 0;
    
    client.Bulk(bd => bd
        .IndexMany(documents, (descriptor, s) => descriptor.Index(indexName).Id(++id)));
    

    产生以下请求

    POST http://localhost:9200/_bulk
    {"index":{"_index":"index-name","_type":"message","_id":1}}
    {"content":"message 1"}
    {"index":{"_index":"index-name","_type":"message","_id":2}}
    {"content":"another message"}
    {"index":{"_index":"index-name","_type":"message","_id":3}}
    {"content":"yet another one"}
    

    【讨论】:

    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2021-10-18
    • 2014-08-27
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    相关资源
    最近更新 更多