Jayaprakash Narayanan
Jayaprakash Narayanan

Reputation: 165

Elasticsearch : AssertionError while getting index name from alias

We have been using Elasticsearch Plugin in our project. While getting index name from alias getting below error

Error

   {
   "error": "AssertionError[Expected current thread[Thread[elasticsearch[Seth][http_server_worker][T#2]{New I/O worker #20},5,main]] to not be a transport thread. Reason: [Blocking operation]]",  "status": 500
   }

Code

    String realIndex = client.admin().cluster().prepareState()
                   .execute().actionGet().getState().getMetaData()
                   .aliases().get(aliasName).iterator()
                   .next().key;

what causes this issue?? Googled it didn't get any help

Upvotes: 0

Views: 252

Answers (1)

Rahul
Rahul

Reputation: 16335

From the look of the error, it seems like this operation is not allowed on the transport thread as it will block the thread until you get the result back. You need to execute this on a execute thread.

public String getIndexName() {
    final IndexNameHolder result = new IndexNameHolder(); // holds the index Name. Needed a final instance here, hence created a holder.
    getTransportClient().admin().cluster().prepareState().execute(new ActionListener<ClusterStateResponse>() {

        @Override
        public void onResponse(ClusterStateResponse response) {
            result.indexName = response.getState().getMetaData().aliases().get("alias").iterator().next().key;

        }
        @Override
        public void onFailure(Throwable e) {
            //Handle failures
        }
    });
    return result.value;
}

There is another method for execute(), one which takes a listener. You need to implement your own listener. In my answer, I have an anonymous implementation of Listener.

I hope it helps

Upvotes: 1

Related Questions