# Update existing document

**URL:** <https://discuss.elastic.co/t/update-existing-document/6776>\
**Category:** Elasticsearch\
**Created:** [February 22, 2012, 12:45am UTC](https://discuss.elastic.co/t/update-existing-document/6776 "2012-02-22T00:45:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_LaRosa](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Frank\_LaRosa](https://discuss.elastic.co/u/Frank_LaRosa)\
**Post date:** [February 22, 2012, 12:45am UTC](https://discuss.elastic.co/t/update-existing-document/6776/1 "2012-02-22T00:45:00Z")

</div>

Hi,

I have some code that works as follows:

- Query for a document using a non-analyzed field (so I should only  
get a result on an exact match)
- If the query returned something, I update a counter in the document  
and re-index it (using prepareIndex with the id value from the hit)
- If the query did not return anything, then I create a new document  
(using prepareIndex without an id)

My expectation is that this code should not produce any documents that  
are duplicates (that have the same value in the non-analyzed field  
used in the original search).

However, that's not the case. I often find several different versions  
of each document.

How is this possible and what can I do to prevent it?

---

<div class="post-metadata">

**Author:** ![lukeforehand\_2](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lukeforehand\_2](https://discuss.elastic.co/u/lukeforehand_2)\
**Post date:** [February 22, 2012, 3:15am UTC](https://discuss.elastic.co/t/update-existing-document/6776/2 "2012-02-22T03:15:02Z")

</div>

Are you indexing while searching, or do you have multiple ClientS  
working at the same time that might cause an inconsistent state of  
your index?

-Luke

On Feb 21, 6:45 pm, Frank LaRosa [fr...@studyblue.com](mailto:fr...@studyblue.com) wrote:

> Hi,
> 
> I have some code that works as follows:
> 
> - Query for a document using a non-analyzed field (so I should only  
> get a result on an exact match)
> - If the query returned something, I update a counter in the document  
> and re-index it (using prepareIndex with the id value from the hit)
> - If the query did not return anything, then I create a new document  
> (using prepareIndex without an id)
> 
> My expectation is that this code should not produce any documents that  
> are duplicates (that have the same value in the non-analyzed field  
> used in the original search).
> 
> However, that's not the case. I often find several different versions  
> of each document.
> 
> How is this possible and what can I do to prevent it?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [February 22, 2012, 8:54am UTC](https://discuss.elastic.co/t/update-existing-document/6776/3 "2012-02-22T08:54:04Z")

</div>

On Tue, 2012-02-21 at 16:45 -0800, Frank LaRosa wrote:

> Hi,
> 
> I have some code that works as follows:
> 
> - Query for a document using a non-analyzed field (so I should only  
> get a result on an exact match)
> - If the query returned something, I update a counter in the document  
> and re-index it (using prepareIndex with the id value from the hit)
> - If the query did not return anything, then I create a new document  
> (using prepareIndex without an id)

Because all of this is happening in parallel, you may well get two  
processes checking for the same (missing) value at the same time, and  
both of them end up creating new docs.

The only way to emulate a unique key in ES is by using the doc ID.

clint

---

<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:** [July 6, 2017, 3:38am UTC](https://discuss.elastic.co/t/update-existing-document/6776/4 "2017-07-06T03:38:35Z")

</div>

Closed on July 6, 2017, 3:38am UTC


