# How to set a id using ElasticClient

**URL:** <https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821>\
**Category:** Elasticsearch\
**Created:** [April 8, 2019, 11:09am UTC](https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821 "2019-04-08T11:09:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frankm/32/43658_2.png) [@frankm](https://discuss.elastic.co/u/frankm)\
**Post date:** [April 8, 2019, 11:09am UTC](https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821/1 "2019-04-08T11:09:59Z")

</div>

To avoid duplicates I like to give a document ID to each data set into Elastic.

As described at the [documentation](https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html) I like to set a ID while adding a item to Elastic. The description recommend to set the ID like this:

> PUT /{index}/{type}/{id}

But I'm using the ElasticClient and I'm adding items like this:

> ElasticClient.IndexDocumentAsync(doc);

I don't know how to set the Index here. I thought about some thing like

> ElasticClient.SetId(id);

but I cant find a method like that.

**How can I add the ID using the IElasticClient object?**

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [April 8, 2019, 11:18am UTC](https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821/2 "2019-04-08T11:18:27Z")

</div>

Assuming you are using java to index documents, [the docs are here](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-document-index.html)

---

<div class="post-metadata">

**Author:** ![frankm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frankm/32/43658_2.png) [@frankm](https://discuss.elastic.co/u/frankm)\
**Post date:** [April 8, 2019, 11:19am UTC](https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821/3 "2019-04-08T11:19:22Z")

</div>

I'm using **C# .NET**.

To set the Id I extend my _\_doc_ class with the property _Id_.  
Than I set the _Id_.

```
        doc.Id = myId;
        ElasticClient.IndexDocumentAsync(doc);

```

So I don't get any duplicates at the Elastic anymore.

---

<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:** [May 6, 2019, 11:19am UTC](https://discuss.elastic.co/t/how-to-set-a-id-using-elasticclient/175821/4 "2019-05-06T11:19:27Z")

</div>

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