# How to avoid "Phantom Read" problem of nested field

**URL:** <https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878>\
**Category:** Elasticsearch\
**Created:** [November 7, 2019, 1:57am UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878 "2019-11-07T01:57:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony21177](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tony21177/32/56161_2.png) [@tony21177](https://discuss.elastic.co/u/tony21177)\
**Post date:** [November 7, 2019, 1:57am UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878/1 "2019-11-07T01:57:52Z")

</div>

Hi,

I have a rdbms table design which are one to many relation.  
Under some consideration,we decide put the relation into elasticsearch index.  
I create a index(for parent table) and a nested field for the child table.  
Everything works fine until the tough "phantom Read" problem occur as below.

Our requirement is that some field in the nested object need to be unique across documents,  
So I need to check the value of the field before adding a new nested object.

Because the verification and insert action are seperate Elasticsearch API,it may cause the "phantom read" due to the other concurrent insertion request.

Following the [Optimistic concurrency control](https://www.elastic.co/guide/en/elasticsearch/reference//7.2/optimistic-concurrency-control.html),we use the if\_seq\_no and if\_primary\_term to avoid the problem.

1. But the unique field in nested object is across all documents and every document has its own seq\_no and primary\_term,How do I assure it won't happen "phantom read" when just adding a nested object into one document?

2. I also notice that the meta field "version" of document [Versioning](https://www.elastic.co/guide/en/elasticsearch/reference//7.2/docs-index_.html#index-versioning),can I also use version for concurrency control?

3. Or concurrency control of seq\_no and primary\_term can be used in distributed elasticsearch node and version field just for one node?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 7, 2019, 9:04am UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878/2 "2019-11-07T09:04:04Z")

</div>

Hey,

always go with seq\_no and primary\_term independent from the number of nodes. One question for me remains: How do you handle refreshes? It may take up to a second, until a written document is made available for search (check the refresh interval setting for more information), so a search may not return a very recently added document. Are you accounting for this?

--Alex

---

<div class="post-metadata">

**Author:** ![tony21177](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tony21177/32/56161_2.png) [@tony21177](https://discuss.elastic.co/u/tony21177)\
**Post date:** [November 7, 2019, 12:51pm UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878/3 "2019-11-07T12:51:55Z")

</div>

Hi,spinscale  
I use refresh=wait\_for.

Let me explain more clear.  
I want to make sure the some field is unique of nested object across all documents,so before inserting a new object,I need to check the value of this field of all nested objects.  
But there may be another insertion request to elasticsearch of nested object between my checking request and insertion request,So the newly added nested object is not include in my checking scope,that is called **phantom read**.

For RDBMS,I can start a transaction and modify the isolation level to serializable to avoid phantom read,But I do not know what the appropriate solution is for Elasticsearch.

Thank you!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 7, 2019, 3:06pm UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878/4 "2019-11-07T15:06:47Z")

</div>

optimistic concurrency control only works on a per document base, but not across all documents, as you already mentioned. However your would need a transaction isolation against all documents to be changed, which does not work with elasticsearch.

---

<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:** [December 5, 2019, 3:06pm UTC](https://discuss.elastic.co/t/how-to-avoid-phantom-read-problem-of-nested-field/206878/5 "2019-12-05T15:06:51Z")

</div>

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