首页 > 编程语言 >ElasticSearch Java API之索引操作

ElasticSearch Java API之索引操作

时间:2022-11-09 15:05:20浏览次数:76  
标签:Java request client 索引 API ElasticSearch 分片 println response


背景:​​ElasticSearch Java 客户端连接ElasticSearch​​以这篇博客为基础

​​ElasticSearch:简单介绍以及使用Docker部署ElasticSearch 和 Kibana​​ 这篇博客简单部署了ElasticSearch

索引概念简单介绍

通常说的索引有两种词性,名称和动词。

  • 动词索引indexing,索引一个文档,表示把一个文档存储到索引Index里,可以用来查询和检索,es采用倒排索引
  • 名词索引index,简单的理解成关系型数据库中的数据库的概念

创建索引

static void createIndex(RestHighLevelClient client){

// 创建索引 - 请求对象
CreateIndexRequest request = new CreateIndexRequest("userxt");
// 发送请求,获取响应
CreateIndexResponse response = null;
try {
response = client.indices().create(request,
RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
boolean acknowledged = response.isAcknowledged();
// 响应状态
System.out.println("操作状态 = " + acknowledged);

}

调用结果,创建索引成功

ElasticSearch Java API之索引操作_ide


更新到Elastic Stack 7.13以上版本的朋友可能注意到了,在默认不开启Elastic 安全功能时,Kibana的搜索结果页面会多出一行提示,建议我们开启ElasticSearch 安全功能。

在个人学习或者内网开放ES+VPN连接的情况下我们完全不需要开启安全功能,其他情况在生产集群中还是建议开启安全选项的。

这是因为没有显式禁用安全选项导致的,也就是说ElasticSearch会提示你是不是忘了启用这个选项,只要在配置文件中显式禁用即可取消这个提示。

在elasticsearch.yml 配置禁用安全选项xpack.security.enabled,之后重启ElasticSearch即可:

xpack.security.enabled: false

重新启动容器

docker restart 容器名/容器ID

查询索引

static void searchIndex(RestHighLevelClient client){

// 查询索引 - 请求对象
GetIndexRequest request = new GetIndexRequest("userxt");
// 发送请求,获取响应
GetIndexResponse response = null;
try {
response = client.indices().get(request,
RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("aliases:"+response.getAliases());
System.out.println("mappings:"+response.getMappings());
System.out.println("settings:"+response.getSettings());
}

调用结果,打印出了mappings,settings和aliases等信息

ElasticSearch Java API之索引操作_ide_02


在 ElasticSearch Head 插件中也可以看见新创建的索引,但是如下图所示,发现ElasticSearch 正处于亚健康状态,有分片尚未分配

ElasticSearch Java API之索引操作_新建索引_03


从上面截图可以看出存在unassigned的分片,新建索引的时候,分片数为1,副本数为1,新建之后集群状态成为yellow,其根本原因是因为集群存在没有启用的副本分片,我们先来看一下官网给出的副本分片的介绍:

副本分片的主要目的就是为了故障转移,正如在 集群内的原理 中讨论的:如果持有主分片的节点挂掉了,一个副本分片就会晋升为主分片的角色。

那么可以看出来副本分片和主分片是不能放到一个节点上面的,可是在只有一个节点的集群里,副本分片没有办法分配到其他的节点上,所以出现所有副本分片都unassigned得情况。因为只有一个节点,如果存在主分片节点挂掉了,那么整个集群理应就挂掉了,不存在副本分片升为主分片的情况。

解决办法就是,在单节点的elasticsearch集群,删除存在副本分片的索引,新建索引的副本都设为0。或者设置多节点的ElasticSearch 集群

删除索引

static void deleteIndex(RestHighLevelClient client){
// 删除索引 - 请求对象
DeleteIndexRequest request = new DeleteIndexRequest("userxt");
// 发送请求,获取响应
AcknowledgedResponse response = null;
try {
response = client.indices().delete(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
// 操作结果
if(response.isAcknowledged()==true){
System.out.println("删除索引成功");
}else{
System.out.println("删除索引失败");
}

}

调用结果

ElasticSearch Java API之索引操作_elasticsearch_04

判断指定索引是否存在

static boolean existsIndex(RestHighLevelClient client,String indexName){
GetIndexRequest request = new GetIndexRequest(indexName);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
try {
return client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {

e.printStackTrace();
return false;
}
}

调用结果

ElasticSearch Java API之索引操作_elasticsearch_05

完整代码

public class ElasticsearchConnect {

public static void main(String[] args) throws IOException {

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("xt", "xt"));

RestClientBuilder restClientBuilder = RestClient.builder(
new HttpHost("IP", 9200,"http"));

// 认证和线程数
restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
int threadCount = 10;
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build());

return httpClientBuilder;
});

// 超时超时设置
restClientBuilder.setRequestConfigCallback(requestConfigCallback -> {
requestConfigCallback.setConnectTimeout(10);
requestConfigCallback.setSocketTimeout(10);
return requestConfigCallback;
});


// 创建客户端对象 虽然已经被弃用,但是了解基本使用还是没有问题 里面封装了RestClient
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);

System.out.println(client);

//创建索引
createIndex(client,"userxt");
//获得指定索引的信息
searchIndex(client,"userxt");
//删除指定索引
deleteIndex(client,"userxt");

if(existsIndex(client,"userxt")==true){
System.out.println("索引"+"userxt"+"存在");
}else{
System.out.println("索引"+"userxt"+"不存在");
}
// 关闭客户端连接
client.close();
}

static void createIndex(RestHighLevelClient client,String indexName){

// 创建索引 - 请求对象
CreateIndexRequest request = new CreateIndexRequest(indexName);
// 发送请求,获取响应
CreateIndexResponse response = null;
try {
response = client.indices().create(request,
RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}

if(response.isAcknowledged()==true){
System.out.println("新建索引"+indexName+"成功");
}else{
System.out.println("新建索引+"+indexName+"失败");
}

}


static void searchIndex(RestHighLevelClient client,String indexName){

// 查询索引 - 请求对象
GetIndexRequest request = new GetIndexRequest("userxt");
// 发送请求,获取响应
GetIndexResponse response = null;
try {
response = client.indices().get(request,
RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("aliases:"+response.getAliases());
System.out.println("mappings:"+response.getMappings());
System.out.println("settings:"+response.getSettings());
}

static boolean existsIndex(RestHighLevelClient client,String indexName){
GetIndexRequest request = new GetIndexRequest(indexName);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
try {
return client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {

e.printStackTrace();
return false;
}
}


static void deleteIndex(RestHighLevelClient client,String indexName){
// 删除索引 - 请求对象
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
// 发送请求,获取响应
AcknowledgedResponse response = null;
try {
response = client.indices().delete(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
// 操作结果
if(response.isAcknowledged()==true){
System.out.println("删除索引+"+indexName+"成功");
}else{
System.out.println("删除索引"+indexName+"失败");
}

}


}

References:

(写博客主要是对自己学习的归纳整理,资料大部分来源于书籍、网络资料、官方文档和自己的实践,整理的不足和错误之处,请大家评论区批评指正。同时感谢广大博主和广大作者辛苦整理出来的资源和分享的知识。)


标签:Java,request,client,索引,API,ElasticSearch,分片,println,response
From: https://blog.51cto.com/u_14020077/5836606

相关文章