ElasticSearch索引不能正常工作/可靠 [英] ElasticSearch index exists not working / reliable

查看:323
本文介绍了ElasticSearch索引不能正常工作/可靠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ElasticSearch的管理客户端上编写一个简单的Java包装器。要测试它,我有一个主要的方法,首先检查索引是否存在(IndicesExistsRequest),如果删除它(DeleteIndexRequest),并再次创建索引。见下面的代码。然而,我一直得到一个IndexAlreadyExistsException。



顺便说一下,我试图从命令提示符开始的节点获取一个客户端(通过简单的键入弹性搜索 )。我已经在nodeBuilder的流畅界面上尝试过各种方法的组合,但我似乎无法获得一个。

  public static void main (String [] args){
ElasticSearchJavaClient esjc = new ElasticSearchJavaClient(nda);
if(esjc.indexExists()){
esjc.deleteIndex();
}
esjc.createIndex();
URL url = SchemaCreator.class.getResource(/ elasticsearch / samples.type.json);
String mappings = FileUtil.getContents(url);
esjc.createType(samples,映射);
}

final客户端esClient;
final IndicesAdminClient adminClient;
final String indexName;

public ElasticSearchJavaClient(String indexName){
this.indexName = indexName;
esClient = nodeBuilder()。clusterName(elasticsearch)。client(true).node()。client();
adminClient = esClient.admin()。indexes();
}

public boolean deleteIndex(){
logger.info(删除索引+ indexName);
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
try {
DeleteIndexResponse response = adminClient.delete(request).actionGet();
if(!response.isAcknowledged()){
throw new Exception(无法删除索引+ indexName);
}
logger.info(Index deleted);
返回true;
} catch(IndexMissingException e){
logger.info(No such index:+ indexName);
返回false;
}
}

public boolean indexExists(){
logger.info(String.format(验证索引的存在\%s\ ,indexName));
IndicesExistsRequest request = new IndicesExistsRequest(indexName);
IndicesExistsResponse response = adminClient.exists(request).actionGet();
if(response.isExists()){
logger.info(Index exists);
返回true;
}
logger.info(No such index);
返回false;
}

public void createIndex(){
logger.info(创建索引+ indexName);
CreateIndexRequest request = new CreateIndexRequest(indexName);
IndicesAdminClient iac = esClient.admin()。indexes();
CreateIndexResponse response = iac.create(request).actionGet();
if(!response.isAcknowledged()){
throw new Exception(无法删除索引+ indexName);
}
logger.info(创建索引);
}


解决方案

解。由于java客户端的调用是异步完成的,因此您必须使用采用动作侦听器的变体。尽管如此,解决方案仍然有一些变化:

  //内部类,因为它刚刚被抛出
//动作侦听器实现来表示
//索引存在
private class ExistsException extends RuntimeException {
}

public boolean exists(){
logger.info(String.format(验证索引的存在\%s\,indexName));
IndicesExistsRequest request = new IndicesExistsRequest(indexName);
try {
adminClient.exists(request,new ActionListener< IndicesExistsResponse>(){
public void onResponse(IndicesExistsResponse response){
if(response.isExists()){
抛出新的ExistsException();
}
}
public void onFailure(Throwable e){
ExceptionUtil.smash(e);
}
});
}
catch(ExistsException e){
return true;
}
返回false;
}


I am writing a simple Java wrapper around ElasticSearch's admin client. To test it I have a main method that first checks if an index exists (IndicesExistsRequest), if so deletes it (DeleteIndexRequest), and creates the index again. See code below. Yet I consistently get an IndexAlreadyExistsException.

By the way I am trying to get a client for the node that you start from the command prompt (by simply typing "elastic search"). I have tried every combination of methods on nodeBuilder's fluent interface, but I can't seem to get one.

public static void main(String[] args) {
    ElasticSearchJavaClient esjc = new ElasticSearchJavaClient("nda");
    if (esjc.indexExists()) {
        esjc.deleteIndex();
    }
    esjc.createIndex();
    URL url = SchemaCreator.class.getResource("/elasticsearch/specimen.type.json");
    String mappings = FileUtil.getContents(url);
    esjc.createType("specimen", mappings);
}

final Client esClient;
final IndicesAdminClient adminClient;
final String indexName;

public ElasticSearchJavaClient(String indexName) {
    this.indexName = indexName;
    esClient = nodeBuilder().clusterName("elasticsearch").client(true).node().client();
    adminClient = esClient.admin().indices();
}

public boolean deleteIndex() {
    logger.info("Deleting index " + indexName);
    DeleteIndexRequest request = new DeleteIndexRequest(indexName);
    try {
        DeleteIndexResponse response = adminClient.delete(request).actionGet();
        if (!response.isAcknowledged()) {
            throw new Exception("Failed to delete index " + indexName);
        }
        logger.info("Index deleted");
        return true;
    } catch (IndexMissingException e) {
        logger.info("No such index: " + indexName);
        return false;
    }
}

public boolean indexExists() {
    logger.info(String.format("Verifying existence of index \"%s\"", indexName));
    IndicesExistsRequest request = new IndicesExistsRequest(indexName);
    IndicesExistsResponse response = adminClient.exists(request).actionGet();
    if (response.isExists()) {
        logger.info("Index exists");
        return true;
    }
    logger.info("No such index");
    return false;
}

public void createIndex() {
    logger.info("Creating index " + indexName);
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    IndicesAdminClient iac = esClient.admin().indices();
    CreateIndexResponse response = iac.create(request).actionGet();
    if (!response.isAcknowledged()) {
        throw new Exception("Failed to delete index " + indexName);
    }
    logger.info("Index created");
}

解决方案

OK, I figured out a solution. Since the java client's calls are done asynchronously you have to use the variant which takes an action listener. The solution still gets a bit contrived though:

// Inner class because it's just used to be thrown out of
// the action listener implementation to signal that the
// index exists
private class ExistsException extends RuntimeException {
}

public boolean exists() {
    logger.info(String.format("Verifying existence of index \"%s\"", indexName));
    IndicesExistsRequest request = new IndicesExistsRequest(indexName);
    try {
        adminClient.exists(request, new ActionListener<IndicesExistsResponse>() {
            public void onResponse(IndicesExistsResponse response) {
                if (response.isExists()) {
                    throw new ExistsException();
                }
            }
            public void onFailure(Throwable e) {
                ExceptionUtil.smash(e);
            }
        });
    }
    catch (ExistsException e) {
        return true;
    }
    return false;
}

这篇关于ElasticSearch索引不能正常工作/可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆