Testing the index existence

I need to test if an index already exists, otherwise I create it by code.
In order to test if an index already exists I execute :
private boolean existsIndex() {
ClusterState cs =
client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(INDICE);
return imd != null;
}
Howerver, somtimes, this test gives a wrong result (the index exists but
the test return false) because the index was not still recovered.
How can avoid this ?
Is there a way to synchronize that operation ?
Any different solution ?
Tks
Tullio

Have you just tried not checking and creating the index, then throwing
an error if you can't create it?

Yes, I know I can, however I prefer to test before than try/catch.
Tks
Tullio

Il giorno mercoledì 13 giugno 2012 15:39:04 UTC+2, Ævar Arnfjörð Bjarmason
ha scritto:

Have you just tried not checking and creating the index, then throwing
an error if you can't create it?

On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:

I need to test if an index already exists, otherwise I create it by
code.
In order to test if an index already exists I execute :
private boolean existsIndex() {
ClusterState cs =
client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(INDICE);
return imd != null;
}
Howerver, somtimes, this test gives a wrong result (the index exists
but the test return false) because the index was not still recovered.
How can avoid this ?

Have you tried the index-exists API?

http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html

Is there a way to synchronize that operation ?
Any different solution ?
Tks
Tullio

Is more or less what I did with my code.
My installation is embedded and I need to use Java API, howewer what I did
is the same, but the problem is a timing problem.
When I test index is not yet recovered and return false even if the index
exists.
Tks
Tullio

Il giorno mercoledì 13 giugno 2012 16:03:38 UTC+2, Clinton Gormley ha
scritto:

On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:

I need to test if an index already exists, otherwise I create it by
code.
In order to test if an index already exists I execute :
private boolean existsIndex() {
ClusterState cs =

client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();

    IndexMetaData imd = cs.getMetaData().index(INDICE); 
    return imd != null; 
} 

Howerver, somtimes, this test gives a wrong result (the index exists
but the test return false) because the index was not still recovered.
How can avoid this ?

Have you tried the index-exists API?

http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html

Is there a way to synchronize that operation ?
Any different solution ?
Tks
Tullio

Hi Tulio,

Have a look at his method :
https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/fr/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L567

HTH
David

Le 13 juin 2012 à 16:07, tullio0106 tbettinazzi@axioma.it a écrit :

Is more or less what I did with my code.
My installation is embedded and I need to use Java API, howewer what I did is
the same, but the problem is a timing problem.
When I test index is not yet recovered and return false even if the index
exists.
Tks
Tullio

Il giorno mercoledì 13 giugno 2012 16:03:38 UTC+2, Clinton Gormley ha
scritto:

On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:
I need to test if an index already exists, otherwise I create it by
code.
In order to test if an index already exists I execute :
private boolean existsIndex() {
ClusterState cs =
client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(INDICE);
return imd != null;
}
Howerver, somtimes, this test gives a wrong result (the index exists
but the test return false) because the index was not still recovered.
How can avoid this ?

Have you tried the index-exists API?

http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html

Is there a way to synchronize that operation ?
Any different solution ?
Tks
Tullio

http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html

http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html

--
David Pilato
http://dev.david.pilato.fr/
Twitter : @dadoonet

On Wed, Jun 13, 2012 at 3:44 PM, tullio0106 tbettinazzi@axioma.it wrote:

Yes, I know I can, however I prefer to test before than try/catch.

Why?

If you have an API that can create an index but throw an exception if
one exists, why wouldn't you use it and instead opt for an API that'll
always have a race condition, ElasticSearch does not guarantee any
transactionality between API calls, so you can't call "index_exists"
and guarantee that it won't exists 10ms later when you try to create
your own index.

You can use the cluster health API and wait for yellow status.

On Wed, Jun 13, 2012 at 3:28 PM, tullio0106 tbettinazzi@axioma.it wrote:

I need to test if an index already exists, otherwise I create it by code.
In order to test if an index already exists I execute :
private boolean existsIndex() {
ClusterState cs =
client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();
IndexMetaData imd = cs.getMetaData().index(INDICE);
return imd != null;
}
Howerver, somtimes, this test gives a wrong result (the index exists but
the test return false) because the index was not still recovered.
How can avoid this ?
Is there a way to synchronize that operation ?
Any different solution ?
Tks
Tullio

Hi David!
I am using your v1.3.0 of the ElasticsearchAbstractClientFactory bean and I have the same problem as explained in the beginning of the post. Since the index is not recovered by the time of trying to create the index, using a prepareExists is also returning false (does not exist) and raising an error. The solution I use is simply to catch the IndexAlreadyExistsException. I am writing this as feedback to your post in case you want to consider it for further versions of your factories.

Ibai