1.项目地址
https://github.com/GuardFTC/elasticsearch-test.git
2.cat常用命令整合
包括查看集群状态,查看集群索引信息、别名信息、节点信息等
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.cat.*;
import co.elastic.clients.elasticsearch.cat.aliases.AliasesRecord;
import co.elastic.clients.elasticsearch.cat.health.HealthRecord;
import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;
import co.elastic.clients.elasticsearch.cat.master.MasterRecord;
import co.elastic.clients.elasticsearch.cat.nodes.NodesRecord;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@SpringBootTest
class ElasticSearchCatAndClusterTest {
@Resource
@Qualifier("primaryElasticsearchClient")
private ElasticsearchClient primaryClient;
@Test
void testCat() throws IOException {
//1.获取cat客户端
ElasticsearchCatClient cat = primaryClient.cat();
//2.获取集群健康状况
HealthResponse health = cat.health();
List<HealthRecord> healthRecords = health.valueBody();
//3.结果校验
Assert.isTrue(ObjectUtil.isNotNull(healthRecords));
Assert.isTrue(1 == healthRecords.size());
HealthRecord healthRecord = healthRecords.get(0);
Assert.isTrue("ftc-es".equals(healthRecord.cluster()));
Assert.isTrue("3".equals(healthRecord.nodeTotal()));
//4.查询主节点信息并校验结果
MasterResponse master = cat.master();
List<MasterRecord> masterRecords = master.valueBody();
Assert.isTrue(ObjectUtil.isNotNull(masterRecords));
Assert.isTrue(1 == masterRecords.size());
//5.查询集群节点信息
NodesResponse nodes = cat.nodes();
List<NodesRecord> nodesRecords = nodes.valueBody();
Assert.isTrue(ObjectUtil.isNotNull(nodesRecords));
Assert.isTrue(3 == nodesRecords.size());
//6.查询集群别名信息
AliasesResponse aliases = cat.aliases();
List<AliasesRecord> aliasesRecords = aliases.valueBody();
Assert.isTrue(ObjectUtil.isNotNull(aliasesRecords));
//7.查询集群索引信息
IndicesResponse indices = cat.indices();
List<IndicesRecord> indicesRecords = indices.valueBody();
Assert.isTrue(ObjectUtil.isNotNull(indicesRecords));
}
}
3.cluster常用命令整合
包括查看集群健康状态,查看集群统计信息,查看集群状态等
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.HealthStatus;
import co.elastic.clients.elasticsearch.cluster.ElasticsearchClusterClient;
import co.elastic.clients.elasticsearch.cluster.StateResponse;
import co.elastic.clients.json.JsonData;
import jakarta.json.JsonObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
@SpringBootTest
class ElasticSearchCatAndClusterTest {
@Resource
@Qualifier("primaryElasticsearchClient")
private ElasticsearchClient primaryClient;
@Test
void testCluster() throws IOException {
//1.获取cluster客户端
ElasticsearchClusterClient cluster = primaryClient.cluster();
//2.查询集群健康状态
co.elastic.clients.elasticsearch.cluster.HealthResponse health = cluster.health();
//3.校验集群健康状态信息
Assert.isTrue(ObjectUtil.isNotNull(health));
Assert.isTrue("ftc-es".equals(health.clusterName()));
Assert.isTrue(3 == health.numberOfNodes());
Assert.isTrue(HealthStatus.Green.jsonValue().equals(health.status().jsonValue()));
//4.查询集群状态
StateResponse state = cluster.state();
JsonData jsonData = state.valueBody();
//5.校验集群状态
Assert.isTrue(ObjectUtil.isNotNull(jsonData));
JsonObject status = jsonData.toJson().asJsonObject();
Assert.isTrue("ftc-es".equals(status.getString("cluster_name")));
Assert.isTrue(3 == status.getJsonObject("nodes").size());
}
}
4.索引常用命令整合
其实上述两个命令的整合,我本人并没有体验到JavaClientApi好用之处。直到索引整合这部分,哇哦!!!!!!这个客户端的API使用起来简直是太曼妙了!!!!!
我唯一想到的词就是曼妙!!!!!JavaClientApi把Elasticsearch的命令用java的形式完美的体现了出来。接下来上代码!!!!
创建索引
使用.indices().create()方法来创建索引。.indices()指定索引路由,.create()方法指定创建索引
创建索引无属性设置
@Test
void testCreateIndex() throws IOException {
//1.创建无参索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
索引别名设置
@Test
void testAlias() throws IOException {
//1.创建无参索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME + "_1"));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME + "_2"));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.多个索引添加一个别名
PutAliasResponse putAliasResponse = secondaryClient
.indices()
.putAlias(a -> a
.index(INDEX_NAME + "_1")
.name(INDEX_NAME)
);
Assert.isTrue(putAliasResponse.acknowledged());
//3.更新索引别名
UpdateAliasesResponse updateAliasesResponse = secondaryClient
.indices()
.updateAliases(i -> i.actions(
Action.of(a -> a.remove(
r -> r.indices(INDEX_NAME + "_1").aliases(INDEX_NAME)
)),
Action.of(a -> a.add(
r -> r.indices(INDEX_NAME + "_2").aliases(INDEX_NAME)
))
));
Assert.isTrue(updateAliasesResponse.acknowledged());
//4.删除别名
DeleteAliasResponse deleteAliasResponse = secondaryClient
.indices()
.deleteAlias(i -> i
.index(INDEX_NAME + "_1", INDEX_NAME + "_2")
.name(CollUtil.newArrayList(INDEX_NAME))
);
Assert.isTrue(deleteAliasResponse.acknowledged());
//5.查询别名
GetAliasResponse alias = secondaryClient
.indices()
.getAlias(i -> i.index(INDEX_NAME + "_1", INDEX_NAME + "_2"));
//6.校验查询结果
Set<String> indexNames = alias.result().keySet();
Assert.isTrue(2 == indexNames.size());
indexNames.forEach(indexName -> {
Set<String> aliases = alias.result().get(indexName).aliases().keySet();
Assert.isTrue(0 == aliases.size());
});
}
创建索引时指定别名
@Test
void testCreateIndexWithAlias() throws IOException {
//1.创建单个别名索引
Alias alias = Alias.of(a -> a);
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, alias)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.创建多个别名索引
Map<String, Alias> aliasMap = MapUtil.newHashMap(2);
aliasMap.put(INDEX_NAME, Alias.of(a -> a));
aliasMap.put(INDEX_NAME + "_bak", Alias.of(a -> a));
createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_2")
.aliases(aliasMap)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//3.查询索引别名
GetAliasResponse aliasResponse = secondaryClient
.indices()
.getAlias(i -> i.index(INDEX_NAME + "_1", INDEX_NAME + "_2"));
//4.校验索引别名
Map<String, IndexAliases> result = aliasResponse.result();
Set<String> aliasNames = result.get(INDEX_NAME + "_1").aliases().keySet();
Assert.isTrue("[\"test_index\"]".equals(JSONUtil.toJsonStr(aliasNames)));
aliasNames = result.get(INDEX_NAME + "_2").aliases().keySet();
Assert.isTrue("[\"test_index_bak\",\"test_index\"]".equals(JSONUtil.toJsonStr(aliasNames)));
}
创建索引时指定mapping
直到这步,哇,真的,这种使用方法太曼妙了!!!!!
@Test
void testCreateIndexWithMapping() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME)
.mappings(m -> m
.properties("name", p -> p.text(t -> t.analyzer("ik_max_word")))
.properties("age", p -> p.integer(in -> in))
.properties("tags", p -> p.keyword(k -> k))
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
创建索引时指定setting
@Test
void testCreateIndexWithSetting() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
创建索引时指定全部参数
@Test
void testCreateIndexWithAllArgs() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, a -> a)
.mappings(m -> m
.properties("name", p -> p.text(t -> t.analyzer("ik_max_word")))
.properties("age", p -> p.integer(in -> in))
.properties("tags", p -> p.keyword(k -> k))
)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
查询索引
@Test
void testGetIndexes() throws IOException {
//1.创建测试索引
CreateIndexResponse response = primaryClient.indices().create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//2.查询全部索引
IndicesResponse indices = primaryClient.cat().indices();
List<String> indexNames = CollStreamUtil.toList(indices.valueBody(), IndicesRecord::index);
Assert.isTrue(indexNames.contains(INDEX_NAME));
//3.查询单个索引
GetIndexResponse getIndexResponse = primaryClient.indices().get(i -> i.index(INDEX_NAME));
Map<String, IndexState> result = getIndexResponse.result();
IndexState indexState = result.get(INDEX_NAME);
Assert.isTrue(ObjectUtil.isNotNull(indexState));
//4.获取索引单个属性
Map<String, Alias> aliases = indexState.aliases();
TypeMapping mappings = indexState.mappings();
IndexSettings settings = indexState.settings();
Assert.isTrue(ObjectUtil.isNotNull(aliases));
Assert.isTrue(ObjectUtil.isNotNull(mappings));
Assert.isTrue(ObjectUtil.isNotNull(settings));
}
验证索引是否存在
@Test
void testExistIndex() throws IOException {
//1.验证是否存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
//2.校验结果
Assert.isFalse(exists.value());
}
删除索引
@Test
void testDeleteIndex() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.验证是否存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
Assert.isTrue(exists.value());
//3.删除索引
DeleteIndexResponse delete = primaryClient
.indices()
.delete(d -> d.index(INDEX_NAME));
Assert.isTrue(delete.acknowledged());
//4.验证是否存在
exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
Assert.isFalse(exists.value());
}
修改索引
在这一步,明确的体会到了,refresh的影响。因为之前是用kibana的Devtool,全程人工,所以refresh那点时间差是完全可以忽略的。
但是一旦到了代码的世界,那这1s简直就是一眼万年了。。。。
@Test
void testUpdateIndex() throws IOException, InterruptedException {
//1.创建老索引
CreateIndexResponse response = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, a -> a)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
)
);
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//2.创建新索引
response = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_2")
.aliases(INDEX_NAME, a -> a)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//3.老索引存入一条文档
JSONObject entries = new JSONObject();
entries.set("age", 1);
IndexResponse index = primaryClient.index(i -> i
.index(INDEX_NAME + "_1")
.document(entries)
);
String id = index.id();
Assert.isTrue(StrUtil.isNotBlank(id));
//4.线程睡一会,给分片一个refresh的时间
TimeUnit.SECONDS.sleep(1);
//5.老索引数据迁移到新索引
ReindexResponse reindex = primaryClient.reindex(r -> r
.source(s -> s.index(INDEX_NAME + "_1"))
.dest(d -> d.index(INDEX_NAME + "_2"))
);
Assert.isTrue(1 == reindex.batches());
//6.删除老索引
DeleteIndexResponse delete = primaryClient.indices().delete(d -> d.index(INDEX_NAME + "_1"));
Assert.isTrue(delete.acknowledged());
//7.验证老索引不存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME + "_1"));
Assert.isFalse(exists.value());
//8.线程再睡一会,给新索引一个refresh的时间
TimeUnit.SECONDS.sleep(1);
//9.查询数据在新索引
SearchResponse<JSONObject> search = primaryClient.search(s -> s
.index(INDEX_NAME + "_2")
, JSONObject.class);
String resultId = search.hits().hits().get(0).id();
Assert.isTrue(id.equals(resultId));
}
完整版单元测试
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.cat.IndicesResponse;
import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.ReindexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.elasticsearch.indices.get_alias.IndexAliases;
import co.elastic.clients.elasticsearch.indices.update_aliases.Action;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@SpringBootTest
class ElasticSearchIndexTest {
@Resource
@Qualifier("primaryElasticsearchClient")
private ElasticsearchClient primaryClient;
@Resource
@Qualifier("secondaryElasticsearchClient")
private ElasticsearchClient secondaryClient;
/**
* 测试Mock索引名称常量
*/
private static final String INDEX_NAME = "test_index";
@BeforeEach
void removeApplicationIndex() throws IOException {
//1.查询所有索引
IndicesResponse catIndies = primaryClient.cat().indices();
List<IndicesRecord> indicesRecords = catIndies.valueBody();
//2.获取所有自定义索引名称
List<String> applicationIndexNames = indicesRecords.stream()
.map(IndicesRecord::index)
.filter(index -> !index.startsWith(StrUtil.DOT))
.collect(Collectors.toList());
//3.判空返回
if (CollUtil.isEmpty(applicationIndexNames)) {
return;
}
//4.删除
ElasticsearchIndicesClient indicesClient = primaryClient.indices();
DeleteIndexResponse delete = indicesClient.delete(i -> i.index(applicationIndexNames));
Assert.isTrue(delete.acknowledged());
}
@Test
void testCreateIndex() throws IOException {
//1.创建无参索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
@Test
void testAlias() throws IOException {
//1.创建无参索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME + "_1"));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
createIndexResponse = secondaryClient
.indices()
.create(i -> i.index(INDEX_NAME + "_2"));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.多个索引添加一个别名
PutAliasResponse putAliasResponse = secondaryClient
.indices()
.putAlias(a -> a
.index(INDEX_NAME + "_1")
.name(INDEX_NAME)
);
Assert.isTrue(putAliasResponse.acknowledged());
//3.更新索引别名
UpdateAliasesResponse updateAliasesResponse = secondaryClient
.indices()
.updateAliases(i -> i.actions(
Action.of(a -> a.remove(
r -> r.indices(INDEX_NAME + "_1").aliases(INDEX_NAME)
)),
Action.of(a -> a.add(
r -> r.indices(INDEX_NAME + "_2").aliases(INDEX_NAME)
))
));
Assert.isTrue(updateAliasesResponse.acknowledged());
//4.删除别名
DeleteAliasResponse deleteAliasResponse = secondaryClient
.indices()
.deleteAlias(i -> i
.index(INDEX_NAME + "_1", INDEX_NAME + "_2")
.name(CollUtil.newArrayList(INDEX_NAME))
);
Assert.isTrue(deleteAliasResponse.acknowledged());
//5.查询别名
GetAliasResponse alias = secondaryClient
.indices()
.getAlias(i -> i.index(INDEX_NAME + "_1", INDEX_NAME + "_2"));
//6.校验查询结果
Set<String> indexNames = alias.result().keySet();
Assert.isTrue(2 == indexNames.size());
indexNames.forEach(indexName -> {
Set<String> aliases = alias.result().get(indexName).aliases().keySet();
Assert.isTrue(0 == aliases.size());
});
}
@Test
void testCreateIndexWithAlias() throws IOException {
//1.创建单个别名索引
Alias alias = Alias.of(a -> a);
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, alias)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.创建多个别名索引
Map<String, Alias> aliasMap = MapUtil.newHashMap(2);
aliasMap.put(INDEX_NAME, Alias.of(a -> a));
aliasMap.put(INDEX_NAME + "_bak", Alias.of(a -> a));
createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_2")
.aliases(aliasMap)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//3.查询索引别名
GetAliasResponse aliasResponse = secondaryClient
.indices()
.getAlias(i -> i.index(INDEX_NAME + "_1", INDEX_NAME + "_2"));
//4.校验索引别名
Map<String, IndexAliases> result = aliasResponse.result();
Set<String> aliasNames = result.get(INDEX_NAME + "_1").aliases().keySet();
Assert.isTrue("[\"test_index\"]".equals(JSONUtil.toJsonStr(aliasNames)));
aliasNames = result.get(INDEX_NAME + "_2").aliases().keySet();
Assert.isTrue("[\"test_index_bak\",\"test_index\"]".equals(JSONUtil.toJsonStr(aliasNames)));
}
@Test
void testCreateIndexWithMapping() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME)
.mappings(m -> m
.properties("name", p -> p.text(t -> t.analyzer("ik_max_word")))
.properties("age", p -> p.integer(in -> in))
.properties("tags", p -> p.keyword(k -> k))
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
@Test
void testCreateIndexWithSetting() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
@Test
void testCreateIndexWithAllArgs() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = secondaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, a -> a)
.mappings(m -> m
.properties("name", p -> p.text(t -> t.analyzer("ik_max_word")))
.properties("age", p -> p.integer(in -> in))
.properties("tags", p -> p.keyword(k -> k))
)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
}
@Test
void testGetIndexes() throws IOException {
//1.创建测试索引
CreateIndexResponse response = primaryClient.indices().create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//2.查询全部索引
IndicesResponse indices = primaryClient.cat().indices();
List<String> indexNames = CollStreamUtil.toList(indices.valueBody(), IndicesRecord::index);
Assert.isTrue(indexNames.contains(INDEX_NAME));
//3.查询单个索引
GetIndexResponse getIndexResponse = primaryClient.indices().get(i -> i.index(INDEX_NAME));
Map<String, IndexState> result = getIndexResponse.result();
IndexState indexState = result.get(INDEX_NAME);
Assert.isTrue(ObjectUtil.isNotNull(indexState));
//4.获取索引单个属性
Map<String, Alias> aliases = indexState.aliases();
TypeMapping mappings = indexState.mappings();
IndexSettings settings = indexState.settings();
Assert.isTrue(ObjectUtil.isNotNull(aliases));
Assert.isTrue(ObjectUtil.isNotNull(mappings));
Assert.isTrue(ObjectUtil.isNotNull(settings));
}
@Test
void testExistIndex() throws IOException {
//1.验证是否存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
//2.校验结果
Assert.isFalse(exists.value());
}
@Test
void testDeleteIndex() throws IOException {
//1.创建索引
CreateIndexResponse createIndexResponse = primaryClient
.indices()
.create(i -> i.index(INDEX_NAME));
Assert.isTrue(Boolean.TRUE.equals(createIndexResponse.acknowledged()));
//2.验证是否存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
Assert.isTrue(exists.value());
//3.删除索引
DeleteIndexResponse delete = primaryClient
.indices()
.delete(d -> d.index(INDEX_NAME));
Assert.isTrue(delete.acknowledged());
//4.验证是否存在
exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME));
Assert.isFalse(exists.value());
}
@Test
void testUpdateIndex() throws IOException, InterruptedException {
//1.创建老索引
CreateIndexResponse response = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_1")
.aliases(INDEX_NAME, a -> a)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
)
);
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//2.创建新索引
response = primaryClient
.indices()
.create(i -> i
.index(INDEX_NAME + "_2")
.aliases(INDEX_NAME, a -> a)
.settings(s -> s
.refreshInterval(r -> r.time("1s"))
.numberOfShards("3")
.numberOfReplicas("1")
)
);
Assert.isTrue(Boolean.TRUE.equals(response.acknowledged()));
//3.老索引存入一条文档
JSONObject entries = new JSONObject();
entries.set("age", 1);
IndexResponse index = primaryClient.index(i -> i
.index(INDEX_NAME + "_1")
.document(entries)
);
String id = index.id();
Assert.isTrue(StrUtil.isNotBlank(id));
//4.线程睡一会,给分片一个refresh的时间
TimeUnit.SECONDS.sleep(1);
//5.老索引数据迁移到新索引
ReindexResponse reindex = primaryClient.reindex(r -> r
.source(s -> s.index(INDEX_NAME + "_1"))
.dest(d -> d.index(INDEX_NAME + "_2"))
);
Assert.isTrue(1 == reindex.batches());
//6.删除老索引
DeleteIndexResponse delete = primaryClient.indices().delete(d -> d.index(INDEX_NAME + "_1"));
Assert.isTrue(delete.acknowledged());
//7.验证老索引不存在
BooleanResponse exists = primaryClient.indices().exists(e -> e.index(INDEX_NAME + "_1"));
Assert.isFalse(exists.value());
//8.线程再睡一会,给新索引一个refresh的时间
TimeUnit.SECONDS.sleep(1);
//9.查询数据在新索引
SearchResponse<JSONObject> search = primaryClient.search(s -> s
.index(INDEX_NAME + "_2")
, JSONObject.class);
String resultId = search.hits().hits().get(0).id();
Assert.isTrue(id.equals(resultId));
}
}