gpt4 book ai didi

java - IndicesExistsResponse.isExists()不适用于嵌入式ElasticSearch实例

转载 作者:行者123 更新时间:2023-12-02 22:41:40 26 4
gpt4 key购买 nike

我正在使用以下Java代码创建ElasticSearch实例的实例并创建一个名为“testindex”的索引。



Node node = NodeBuilder.nodeBuilder().settings(ImmutableSettings.settingsBuilder()
.put("path.data", "/etc/elasticsearch")
.put("cluster.name", "testcluster"))
.node();
Client client = node.client();
IndicesAdminClient indices = client.admin().indices();
IndicesExistsResponse res = indices.prepareExists("testindex").execute().actionGet();
if(!res.isExists()){ //Everytime getting value as false
CreateIndexRequestBuilder createIndexRequestBuilder = indices.prepareCreate("testindex");
createIndexRequestBuilder.execute().actionGet(); // Erring out with IndexAlreadyExistsException
}


在创建索引之前,我要检查索引是否存在,我只会创建索引(如果存在)。
第二次当我运行上面的代码时,它失败并出现IndexAlreadyExistsException。
似乎IndicesExistsResponse.isExists()的行为不正确。

如果在类路径中存在默认的elasticsearch.yml文件,则以上代码可以正常工作。如果elasticsearch.yml在类路径中,则IndicesExistResponse.isExists()返回true。

有解决此问题的解决方法吗?

最佳答案

请看一下:

if(res.isExists()){
...
}

您要创建索引(如果存在)。

关于java - IndicesExistsResponse.isExists()不适用于嵌入式ElasticSearch实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com