当前位置: 首页 > 面试题库 >

ElasticSearch索引不起作用/可靠

陈开宇
2023-03-14
问题内容

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

顺便说一句,我正在尝试为您从命令提示符启动的节点获取客户端(只需键入“elasticsearch”)。我已经在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/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");
}

问题答案:

好,我想出了一个解决方案。由于Java客户端的调用是异步完成的,因此您必须使用带有操作侦听器的变体。解决方案仍然有些人为的:

// 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版本是ES2.2。我为全文搜索键入与官方教程相同的代码。(https://www.elastic.co/guide/en/elasticsearch/guide/current/match-query.html) 看来全文对我不起作用。我的设定有什么问题?谢谢! 我键入的代码如下: 返回的结果是: 当我输入精确的查询时,我只能得到一次命中,该查询存储在索引中。 输

  • 问题内容: 我试图让MongoDB根据其索引检测重复值。我认为这在MongoDB中是可能的,但是通过Mongoose包装器,事情似乎被打破了。所以对于这样的事情: 我可以用同一封电子邮件保存2个用户。真是 在这里也表达了同样的问题:https : //github.com/LearnBoost/mongoose/issues/56,但是该线程很旧,导致无处可去。 现在,我正在手动调用数据库以查找用

  • Elasticsearch版本():5.2.2 JVM版本():1.8.0_121 OS版本(如果在类UNIX系统上):opensuse 使用“curl-xget'localhost:9200/_search?pretty&timeout=1ms'”进行搜索 响应部分为:{“Take”:5,“timed_out”:false,“_shards”:{“total”:208,“successed”:2

  • 问题内容: 我正在尝试为各个字段使用不同的分析器设置ElasticSearch索引。但是,我似乎找不到一种设置特定于字段的分析器的方法。这是我创建(测试)索引的方法: 如果我正确阅读了文档,则应创建类型为“ tweet”的索引“ twitter”,并且应通过雪球词根分析器分析“ message”字段的内容。为了对此进行测试,我尝试了以下查询: 如果我没记错的话,那应该会受到打击,因为战斗是战斗的源

  • 问题内容: 我有ES 2.2.0,我正在尝试 但我明白了 这是文件jnk.json的内容 所以我要创建一个映射,然后发布一个文档。我究竟做错了什么? 我得到相同的结果 编辑 非常感谢@Bahaaldine Azarmi!有一个缺少的逗号,我能够分别创建映射:),但我尝试使用bulk命令作为 按照API,它给了我错误 这是我的post.json 我的语法有问题吗?哪个角色不合适? 固定 批量api中

  • 问题内容: 表结构: 总行数: 137967 带有更多表联接的真实查询要更长得多,关键是,我无法使表使用索引。如果要选择特定日期之前的所有数据,这对我来说将很难。但是,我注意到,如果选择较小的数据子集,则可以使MySQL使用索引。 那么,无论我放置什么日期,该如何确保MySQL将使用索引? 问题答案: 一切都按预期进行。:) 有索引可以加快检索速度。他们使用索引查找来完成。 在你第一次查询中不使用