2016-03-21 32 views
0

我们在我们的项目中一直在使用Elasticsearch Plugin。虽然从别名得到以下错误Elasticsearch:从别名获取索引名称时发生AssertionError

错误

{ 
    "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 
    } 

代码

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

是什么原因导致这个问题得到索引名?谷歌搜索没有得到任何帮助

回答

1

从错误的外观看来,这个操作似乎不允许在传输线程上,因为它会阻塞线程,直到你得到结果为止。您需要在执行线程上执行此操作。

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; 
} 

还有另一种方法​​,其中一个需要一个监听器。你需要实现你自己的监听器。在我的回答中,我有一个匿名的Listener实现。

我希望它能帮助

+0

我想这一点,可以从别名值获得索引名,但之后仍得到相同的错误 –

+0

我没有得到这个。获取索引名称后出错?在哪里,它是完全相同的错误。? – Rahul

+0

从环境变量中删除** _ JAVA_OPTIONS = -ea **后,一切正常。即使我可以运行没有上述代码。有时候在使用** ElasticsearchIntegrationTest **运行单元测试时,我被迫在环境变量中添加** _ JAVA_OPTIONS **。需要调试为什么它需要在Elasticsearch单元测试中,为什么它会在插件中导致错误? –