# Waiting for Primary Shard Allocation before closing an Index

**URL:** <https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618>\
**Category:** Elasticsearch\
**Created:** [November 27, 2013, 2:48pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618 "2013-11-27T14:48:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Meiwald](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Stefan\_Meiwald](https://discuss.elastic.co/u/Stefan_Meiwald)\
**Post date:** [November 27, 2013, 2:48pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/1 "2013-11-27T14:48:12Z")

</div>

Hey,

I am wondering if there is a way to wait for the Primary Shard Allocation  
before I try to close an Index with the Java API. My usecase is to create  
an index and close it immediately afterwards.  
If I run both Java statements in a direct sequence I get often the  
Exception: class  
org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException  
Is there any way to wait for the primary shard allocation when I try to  
close the Index? Something like setRefresh(true) when indexing new  
documents?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 27, 2013, 2:53pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/2 "2013-11-27T14:53:10Z")

</div>

I can't think of a good reason you'd want to close immediately but if you  
really do the simplest thing is to wait for the cluster health to turn  
yellow. You can be more careful and just wait for the index health to turn  
yellow but I don't know if there is a simple wait api for that. There is  
for cluster state:

$ curl -XGET '[http://localhost:9200/\_cluster/health?wait\_for\_status=yellow&timeout=50s](http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=50s)'

You can run the same thing in Java code, I'm sure.

On Wed, Nov 27, 2013 at 9:48 AM, Stefan Meiwald [st.meiwald@gmail.com](mailto:st.meiwald@gmail.com)wrote:

> Hey,
> 
> I am wondering if there is a way to wait for the Primary Shard Allocation  
> before I try to close an Index with the Java API. My usecase is to create  
> an index and close it immediately afterwards.  
> If I run both Java statements in a direct sequence I get often the  
> Exception: class  
> org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException  
> Is there any way to wait for the primary shard allocation when I try to  
> close the Index? Something like setRefresh(true) when indexing new  
> documents?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3s%2B2MGEfbjKX9YGFqpNvUVcmDFrUsDaqtZjqwDLP%2Batg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3s%2B2MGEfbjKX9YGFqpNvUVcmDFrUsDaqtZjqwDLP%2Batg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 27, 2013, 2:56pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/3 "2013-11-27T14:56:13Z")

</div>

Same for me. I don't see a reason (may be unit test?) to do that.

Wait for yellow in Java could be done like this:

node.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();

## HTH

David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 27 novembre 2013 at 15:53:34, Nikolas Everett ([nik9000@gmail.com](mailto:nik9000@gmail.com)) a écrit:

I can't think of a good reason you'd want to close immediately but if you really do the simplest thing is to wait for the cluster health to turn yellow. You can be more careful and just wait for the index health to turn yellow but I don't know if there is a simple wait api for that. There is for cluster state:

$ curl -XGET '[http://localhost:9200/\_cluster/health?wait\_for\_status=yellow&timeout=50s](http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=50s)'  
You can run the same thing in Java code, I'm sure.

On Wed, Nov 27, 2013 at 9:48 AM, Stefan Meiwald [st.meiwald@gmail.com](mailto:st.meiwald@gmail.com) wrote:  
Hey,

## I am wondering if there is a way to wait for the Primary Shard Allocation before I try to close an Index with the Java API. My usecase is to create an index and close it immediately afterwards. If I run both Java statements in a direct sequence I get often the Exception: class org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException Is there any way to wait for the primary shard allocation when I try to close the Index? Something like setRefresh(true) when indexing new documents?

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/52f4dee3-5fa9-46e3-a824-513c04afd60e%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3s%2B2MGEfbjKX9YGFqpNvUVcmDFrUsDaqtZjqwDLP%2Batg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3s%2B2MGEfbjKX9YGFqpNvUVcmDFrUsDaqtZjqwDLP%2Batg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.5296080d.519b500d.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.5296080d.519b500d.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Stefan\_Meiwald](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Stefan\_Meiwald](https://discuss.elastic.co/u/Stefan_Meiwald)\
**Post date:** [November 27, 2013, 3:05pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/4 "2013-11-27T15:05:27Z")

</div>

Thanks for that workaround.  
Actually I want to block writing for that index for the first 10 minutes  
and afterwards allow it.  
I just found additional Index Settings that could help me out with that  
issue without closing and opening the index:

index.blocks.read  
index.blocks.write

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Am Mittwoch, 27. November 2013 15:48:12 UTC+1 schrieb Stefan Meiwald:

> Hey,
> 
> I am wondering if there is a way to wait for the Primary Shard Allocation  
> before I try to close an Index with the Java API. My usecase is to create  
> an index and close it immediately afterwards.  
> If I run both Java statements in a direct sequence I get often the  
> Exception: class  
> org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException  
> Is there any way to wait for the primary shard allocation when I try to  
> close the Index? Something like setRefresh(true) when indexing new  
> documents?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/00d5112a-40c3-44ae-ad02-85921dbb4d70%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00d5112a-40c3-44ae-ad02-85921dbb4d70%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 27, 2013, 3:15pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/5 "2013-11-27T15:15:33Z")

</div>

Why not creating the index only when you will start need it?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 27 novembre 2013 at 16:05:32, Stefan Meiwald ([st.meiwald@gmail.com](mailto:st.meiwald@gmail.com)) a écrit:

Thanks for that workaround.  
Actually I want to block writing for that index for the first 10 minutes and afterwards allow it.  
I just found additional Index Settings that could help me out with that issue without closing and opening the index:

index.blocks.read  
index.blocks.write  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html)

Am Mittwoch, 27. November 2013 15:48:12 UTC+1 schrieb Stefan Meiwald:  
Hey,

## I am wondering if there is a way to wait for the Primary Shard Allocation before I try to close an Index with the Java API. My usecase is to create an index and close it immediately afterwards. If I run both Java statements in a direct sequence I get often the Exception: class org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException Is there any way to wait for the primary shard allocation when I try to close the Index? Something like setRefresh(true) when indexing new documents?

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/00d5112a-40c3-44ae-ad02-85921dbb4d70%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/00d5112a-40c3-44ae-ad02-85921dbb4d70%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52960c96.257130a3.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52960c96.257130a3.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Stefan\_Meiwald](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Stefan\_Meiwald](https://discuss.elastic.co/u/Stefan_Meiwald)\
**Post date:** [November 27, 2013, 3:23pm UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/6 "2013-11-27T15:23:26Z")

</div>

I thought I could get around another Index/datastore that holds  
"placeholder" indices for the first 10 minutes but it seems that this would  
be the best solution maybe.

Am Mittwoch, 27. November 2013 15:48:12 UTC+1 schrieb Stefan Meiwald:

> Hey,
> 
> I am wondering if there is a way to wait for the Primary Shard Allocation  
> before I try to close an Index with the Java API. My usecase is to create  
> an index and close it immediately afterwards.  
> If I run both Java statements in a direct sequence I get often the  
> Exception: class  
> org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException  
> Is there any way to wait for the primary shard allocation when I try to  
> close the Index? Something like setRefresh(true) when indexing new  
> documents?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6a3f2681-d7c2-4fd6-a98a-90762b09c0d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6a3f2681-d7c2-4fd6-a98a-90762b09c0d0%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Nicholas\_Shook](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicholas_shook/32/1868_2.png) [@Nicholas\_Shook](https://discuss.elastic.co/u/Nicholas_Shook)\
**Post date:** [January 22, 2014, 1:37am UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/7 "2014-01-22T01:37:15Z")

</div>

I've found that refreshing the index after I create it and before I close  
it solves this issue. This may not be 'best practice'

On Wednesday, November 27, 2013 7:23:26 AM UTC-8, Stefan Meiwald wrote:

> I thought I could get around another Index/datastore that holds  
> "placeholder" indices for the first 10 minutes but it seems that this would  
> be the best solution maybe.
> 
> Am Mittwoch, 27. November 2013 15:48:12 UTC+1 schrieb Stefan Meiwald:
> 
> > Hey,
> > 
> > I am wondering if there is a way to wait for the Primary Shard Allocation  
> > before I try to close an Index with the Java API. My usecase is to create  
> > an index and close it immediately afterwards.  
> > If I run both Java statements in a direct sequence I get often the  
> > Exception: class  
> > org.elasticsearch.indices.IndexPrimaryShardNotAllocatedException  
> > Is there any way to wait for the primary shard allocation when I try to  
> > close the Index? Something like setRefresh(true) when indexing new  
> > documents?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/37e84108-ebe6-4cb5-8fe3-6922d6434315%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/37e84108-ebe6-4cb5-8fe3-6922d6434315%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:55am UTC](https://discuss.elastic.co/t/waiting-for-primary-shard-allocation-before-closing-an-index/14618/8 "2017-07-06T01:55:28Z")

</div>


