在ElasticSearch中使用Bulk.IndexMany指定_id字段 [英] Specify the _id field using Bulk.IndexMany in ElasticSearch

查看:773
本文介绍了在ElasticSearch中使用Bulk.IndexMany指定_id字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm facing a problem inserting document using bulk API (C# NEST v5.4). I've an array of documents and inside of the array I've my ID.

我的代码是:

documents = documents .ToArray();

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

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

How can i insert the _id manually using the descriptor?

提前谢谢!

推荐答案

您可以类似于在BulkDescriptor上设置索引名称的方式设置_id.鉴于以下POCO

You can set _id similarly to how you're setting the index name on the BulkDescriptor. Given the following POCO

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

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

Setting the ids using an incrementing counter for example

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"}

这篇关于在ElasticSearch中使用Bulk.IndexMany指定_id字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆