# How do I ensure an index is the first for a document?

**URL:** <https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349>\
**Category:** Elasticsearch\
**Created:** [September 13, 2019, 3:20am UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349 "2019-09-13T03:20:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcavalieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jcavalieri/32/54206_2.png) [@jcavalieri](https://discuss.elastic.co/u/jcavalieri)\
**Post date:** [September 13, 2019, 3:20am UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/1 "2019-09-13T03:20:42Z")

</div>

In a concurrent system, I believe I understand optimistic locking and passing the `version` query string parameter along. However, that is only if the document exists. I.e. you can't send `?version=0`. What is the equivalent of optimistic locking for the initial indexing of a document?

Thanks,  
John

---

<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:** [September 13, 2019, 5:34am UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/2 "2019-09-13T05:34:44Z")

</div>

You can send a [create document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type). This will fail if it already exists.

---

<div class="post-metadata">

**Author:** ![jcavalieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jcavalieri/32/54206_2.png) [@jcavalieri](https://discuss.elastic.co/u/jcavalieri)\
**Post date:** [September 14, 2019, 2:00pm UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/3 "2019-09-14T14:00:29Z")

</div>

Hi Christian,

If I'm reading the documentation correctly that is only when not specifying an id. Which in our system we have our own ids. It that how you understand how createdocument works as well?

-John

---

<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:** [September 14, 2019, 2:32pm UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/4 "2019-09-14T14:32:42Z")

</div>

If you send without an ID the document should always get created as no collisions can occur. This is for then you do specify an external ID. The following should show this:

```
# Create initial version
POST /test/_create/1
{
  "VERSION": 1
}

# Should fail
POST /test/_create/1
{
  "VERSION": 2
}

# Here we specify type and this should work
POST /test/_doc/1
{
  "VERSION": 2
}
```

---

<div class="post-metadata">

**Author:** ![jcavalieri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jcavalieri/32/54206_2.png) [@jcavalieri](https://discuss.elastic.co/u/jcavalieri)\
**Post date:** [September 16, 2019, 7:25pm UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/5 "2019-09-16T19:25:56Z")

</div>

Oh, nice. This helped.

One thing, the notation I used was slightly different.

```auto
POST /test/doc/1/_create

```

Thanks for the help!

---

<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:** [October 14, 2019, 7:30pm UTC](https://discuss.elastic.co/t/how-do-i-ensure-an-index-is-the-first-for-a-document/199349/6 "2019-10-14T19:30:00Z")

</div>

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