文章目录
摘要
elasticsearch-rest-high-level-client操作elasticsearch
闲的无聊,于是写了这一篇爽文,米娜桑可直接用,除非几乎不可能有bug,有bug当我没说(doge)
QA:无想的一刀欧为啥不用springboot封装的操作依赖涅?
欧认为springboot对操作类过度封装,实现普通简单操作还行,但是涉及到较为复杂的操作时,难以使用,尤其是不同版本的springboot推出的api变化频繁,更加难以使用,es官方推出的api更新不会让操作类变化太频繁,个人感觉spboot操作不如es官方推出的api灵活强大,之前在工作中遇到的需求使用springboot提供的报错难以琢磨,且难以满足需求,所以使用了官方api
elasticsearch版本:7.4
安装操作文档:https://blog.youkuaiyun.com/UnicornRe/article/details/121747039?spm=1001.2014.3001.5501
依赖
依赖最好保持与es版本一致,如果以下依赖报错,在maven < parent > 同级标签旁加上
<properties>
<java.version>1.8</java.version>
<!-- <spring-cloud.version>2020.0.2</spring-cloud.version> -->
<!--解决版本问题-->
<elasticsearch.version>7.4.0</elasticsearch.version>
</properties>
<!--elasticsearch-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
yml配置
可自行修改配置和代码增加多台es机器,address逗号隔开
elasticsearch:
schema: http
address: 192.168.52.43:9200
connectTimeout: 5000
socketTimeout: 5000
connectionRequestTimeout: 5000
maxConnectNum: 100
maxConnectPerRoute: 100
连接配置
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class EsHighLevalConfigure {
//协议
@Value("${elasticsearch.schema:http}")
private String schema="http";
// 集群地址,如果有多个用“,”隔开
@Value("${elasticsearch.address}")
private String address;
// 连接超时时间
@Value("${elasticsearch.connectTimeout:5000}")
private int connectTimeout;
// Socket 连接超时时间
@Value("${elasticsearch.socketTimeout:10000}")
private int socketTimeout;
// 获取连接的超时时间
@Value("${elasticsearch.connectionRequestTimeout:5000}")
private int connectionRequestTimeout;
// 最大连接数
@Value("${elasticsearch.maxConnectNum:100}")
private int maxConnectNum;
// 最大路由连接数
@Value("${elasticsearch.maxConnectPerRoute:100}")
private int maxConnectPerRoute;
@Bean
public static RestHighLevelClient restHighLevelClient() {
List<HttpHost> hostLists = new ArrayList<>();
String[] hostList = address.split(",");
for (String addr : hostList) {
String host = addr.split(":")[0];
String port = addr.split(":")[1];
hostLists.add(new HttpHost(host, Integer.parseInt(port), schema));
}
HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{
});
// 构建连接对象
RestClientBuilder builder = RestClient.builder(httpHost);
// 异步连接延时配置
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeout);
requestConfigBuilder.setSocketTimeout(socketTimeout);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
return requestConfigBuilder;
});
// 异步连接数配置
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
httpClientBuilder.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis());
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
}
索引结构
虽然索引结构肯定不是和你们一样的,但是代码结构不需要伤经动骨,
我来简单说说这个结构吧,一条知识产权信息內包含n个文档annex,包含n个(申请人发明人)applicant,
所以使用了 “type”: “nested"嵌套类型,不晓得与"type”: "object"区别的小伙伴自行学习吧,这里就不多说了。
想要学习部分优化的,安装,数据迁移冷备份的可以看看我的文章:(东西太多,部分就没写)https://blog.youkuaiyun.com/UnicornRe/article/details/121747039?spm=1001.2014.3001.5501
PUT /intellectual
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
PUT /intellectual/_mapping
{
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"type": {
"type": "keyword"
},
"keycode": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"officeId": {
"type": "keyword"
},
"officeName": {
"type": "keyword"
},
"titular": {
"type": "keyword"
},
"applyTime": {
"type": "long"
},
"endTime": {
"type": "long"
},
"status": {
"type": "keyword"
},
"agentName": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"annex": {
"type": "nested",
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"createTime": {
"type": "long"
}
}
},
"applicant": {
"type": "nested",
"properties": {

本文详细介绍了如何使用elasticsearch-rest-high-level-client库进行CRUD操作,包括非嵌套结构的基本增删改查,以及嵌套类型(如nested applicant和annex)的高级操作,如painless脚本更新和搜索高亮。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



