Check if index exists

Hi,

I would like to create a new index if not already there with Java API.
The appended way does not work because the map doesn't contain the
index even if it exists!?

Also the following index map is empty:
map = client.admin().cluster().state(new
ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();

What am I doing wrong?? I need this for unit tests where automatic
index creation does not work in some cases, where I am querying before
indexing ...

Regards,
Peter.

Map map = client.admin().cluster().health(new
ClusterHealthRequest(indexName)).actionGet().getIndices();
// map is always empty!!??
boolean exists = map.containsKey(indexName))
if(!exists) {
client.admin().indices().create(new
CreateIndexRequest(indexName)).actionGet();
// waitForYellow
client.admin().cluster().health(new
ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();
}

Putting the waitForYellowStatus before the ClusterHealthRequest seems
to solve this.

Is this the correct way to do it?

On 22 Jan., 15:41, Karussell tableyourt...@googlemail.com wrote:

Hi,

I would like to create a new index if not already there with Java API.
The appended way does not work because the map doesn't contain the
index even if it exists!?

Also the following index map is empty:
map = client.admin().cluster().state(new
ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();

What am I doing wrong?? I need this for unit tests where automatic
index creation does not work in some cases, where I am querying before
indexing ...

Regards,
Peter.

Map map = client.admin().cluster().health(new
ClusterHealthRequest(indexName)).actionGet().getIndices();
// map is always empty!!??
boolean exists = map.containsKey(indexName))
if(!exists) {
client.admin().indices().create(new
CreateIndexRequest(indexName)).actionGet();
// waitForYellow
client.admin().cluster().health(new
ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();

}

Yea, you should wait for the state to be recovered from the gateway (at least the simple one where indices are recreated).

If you try and create an index, it will block until the cluster is in such a state where the metadata has been recovered, and only then try and create that index. Then, you will either get an exception (that the index already exists) or success.
On Saturday, January 22, 2011 at 4:55 PM, Karussell wrote:

Putting the waitForYellowStatus before the ClusterHealthRequest seems
to solve this.

Is this the correct way to do it?

On 22 Jan., 15:41, Karussell tableyourt...@googlemail.com wrote:

Hi,

I would like to create a new index if not already there with Java API.
The appended way does not work because the map doesn't contain the
index even if it exists!?

Also the following index map is empty:
map = client.admin().cluster().state(new
ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();

What am I doing wrong?? I need this for unit tests where automatic
index creation does not work in some cases, where I am querying before
indexing ...

Regards,
Peter.

Map map = client.admin().cluster().health(new
ClusterHealthRequest(indexName)).actionGet().getIndices();
// map is always empty!!??
boolean exists = map.containsKey(indexName))
if(!exists) {
client.admin().indices().create(new
CreateIndexRequest(indexName)).actionGet();
// waitForYellow
client.admin().cluster().health(new
ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();

}