4

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.

My code is:

documents = documents .ToArray();

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

How can i insert the _id manually using the descriptor?

Thanks in advance!

NatsuDragonEye
  • 109
  • 5
  • 18

2 Answers2

11

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

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)));

yields the following request

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"}
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • I've another question about elasticsearch. I'm quite new to this. If you can check this thread (it's about Reindex) https://stackoverflow.com/questions/45266969/reindexing-using-nest-v5-4-elasticsearch , I appreciate!! Thanks :) – NatsuDragonEye Jul 25 '17 at 13:10
1

Here's an example based on a field of type GUID, but it could be any other type. We can specify the field of the class we want to be the _id of the document in elasticsearch.

public class Customer 
{
    public Guid CustomerId { get; set; }
    public int CustomerCode { get; set; }
    public string Name { get; set; }
}

Example based on the above POCO

public void InsertMany(IList<Customer> customers)
{
    var response = client.Bulk(bd => bd
        .Index("customer")
        .IndexMany(customers, (descriptor, lead) => descriptor.Id(lead.CustomerId)));

    if (response.ServerError != null)
        throw new Exception(response.ServerError?.ToString(), response.OriginalException);
}

The same code asynchronously

public async Task InsertManyAsync(IList<Customer> customers)
{
    var response = await client.BulkAsync(bd => bd
        .Index("customer")
        .IndexMany(customers, (descriptor, lead) => descriptor.Id(lead.CustomerId)));

    if (response.ServerError != null)
        throw new Exception(response.ServerError?.ToString(), response.OriginalException);
}