Get elastic shards failure upon search query across multiple indices (while creating indices)

I'm using Elastic 7.13.1. I create indices dynamically using a template based on the year of a timestamp field. So when I try to index a document with a timestamp for 2023-01-01 into index index-2023 the index will be created via the template if missing. I create the index with 5 shards and default number of replicas.

I also query multiple indices at once using the wildcard index-*. I use Go and the (v7.0.32) client library.

I am running a test with a clean elastic setup (no indices created) that run a go routine to index one document per year (up to N years) and one go routine that searches for some documents (using the wildcard indices). I parse the result for shard failures and often get failures like:


Suggesting to me that while searching the wildcard indices the new index is created by the template but not "yet" fully available. If my assumption is correct, is there a way to prevent such shard failure (e.g. wait that all shards are available before letting the newly created index available for searches)?

Is there something similiar to the wait_for_active_shards query parameter when using the create index API, but for index templates?

I was suggested to refresh the index after indexing into it and it works, or to create the indices beforehand, but I was wondering if there is any other way to "wait" the shards to be available.

Thank you!