Throw error when doc id already exists in index with NEST

Hello,

I'm using BulkAll-method when indexing documents. Everything works fine but I wonder if there's a way too report back if an document with same document id already exists in index.

Is there any?

Yes, if you use a create operation instead of an index the operation will fail if the document id is already present in the index.

1 Like

Now i got another behavior! Thank you (And extra +1 for answering at this late hour) ! :slight_smile:

I tried following code to test this, if someone else is intrested:

    internal class OrderExample
    {
        public string OrderId { get; set; }
        public string Customer { get; set; }
        public DateTimeOffset OrderDate { get; set; }

        public OrderExample(string orderId, string customer)
        {
            OrderId = orderId;
            Customer = customer;
        }
    }
        var orders = new List<OrderExample>();

        orders.Add(new OrderExample("1", "Customer1"));
        orders.Add(new OrderExample("1", "Customer1"));
        orders.Add(new OrderExample("2", "Customer1"));

        var bulkIndexResponse = client.Bulk(b => b
            .CreateMany(orders, (descriptor, order) => descriptor
                .Index($"test-order-data-{order.Customer.ToLower()}")
                .Id($"{order.Customer.ToLower()}_{order.OrderId}")
            ));

        if (bulkIndexResponse.Errors)
        {
            foreach (var item in bulkIndexResponse.ItemsWithErrors)
            {
                Console.WriteLine($"{item.Error.Reason}");
            }
        }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.