# Throw error when doc id already exists in index with NEST

**URL:** https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283
**Category:** Elasticsearch
**Created:** [October 23, 2022, 6:31pm UTC](https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283 "2022-10-23T18:31:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johanwallenborg](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@johanwallenborg](https://discuss.elastic.co/u/johanwallenborg)
#### Post date: [October 23, 2022, 6:31pm UTC](https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283/1 "2022-10-23T18:31:16Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [October 23, 2022, 6:40pm UTC](https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283/2 "2022-10-23T18:40:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![johanwallenborg](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@johanwallenborg](https://discuss.elastic.co/u/johanwallenborg)
#### Post date: [October 23, 2022, 7:29pm UTC](https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283/3 "2022-10-23T19:29:37Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> 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.

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

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

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

```

```auto
        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}");
            }
        }

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 20, 2022, 7:30pm UTC](https://discuss.elastic.co/t/throw-error-when-doc-id-already-exists-in-index-with-nest/317283/4 "2022-11-20T19:30:01Z")

</div>

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