SpringBoot + ES basic project construction example
[Search series] ES basic project construction
I haven’t written ES-related blog posts before, and now I’m starting to make up lessons. It is expected that 5-6 blog posts will show the use of es to your friends; this article will be the first blog post of es combined with springboot, and the basic project environment will be built.
<!-- more -->
I. Project Construction
1. Project dependencies
This project is developed with the help of SpringBoot 2.2.1.RELEASE
+ maven 3.5.3
+ IDEA
Open a web service for testing
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
</dependencies>
2. Configuration Information
Configuration file application.yml, pay attention to the following configuration information, the following is the way to parse the configuration by ourselves
elasticsearch:
host: localhost
port: 9200
user: elastic
pwd: test123
connTimeout: 3000
socketTimeout: 5000
connectionRequestTimeout: 500
Description
The above configuration describes a basic es document operation posture, which is more flexible to use than the better spring-boot-starter-data-elasticsearch
II. SpringBoot combined with ES
1. RestHighLevelClient initialization
Next, we operate es based on RestHighLevelClient
. The first step is to initialize this instance.
@Getter
@Configuration
public class ElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.connTimeout}")
private int connTimeout;
@Value("${elasticsearch.socketTimeout}")
private int socketTimeout;
@Value("${elasticsearch.connectionRequestTimeout}")
private int connectionRequestTimeout;
@Value("${elasticsearch.user}")
private String user;
@Value("${elasticsearch.pwd}")
private String pwd;
@Bean(destroyMethod = "close", name = "client")
public RestHighLevelClient initRestClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(connTimeout)
.setSocketTimeout(socketTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout));
return new RestHighLevelClient(builder);
}
}
Note that in the above implementation, the username + password is not used. When the username and password are set in es, authentication is performed based on the Basic Auth method in the request header for each request; it will be introduced later.
2. Basic use
We built an es on this machine for simulation testing. After the above configuration, we can directly interact with es
es installation can refer to:
- 200605-Centos Install ElasticSearch - A Grey Blog
- 210329-Elastic & Kibana Installation and Basic Use - A Grey Blog
docker install:docker pull docker.elastic.co/elasticsearch/elasticsearch:xxx
The following is a simple posture to use
@Service
public class EsTest {
@Autowired
private RestHighLevelClient client;
private static String auth;
public EsTest(ElasticsearchConfiguration elasticsearchConfiguration) {
auth = Base64Utils.encodeToString((elasticsearchConfiguration.getUser() + ":" + elasticsearchConfiguration.getPwd()).getBytes());
auth = "Basic " + auth;
}
public void testGet() throws Exception {
// 文档查询
GetRequest getRequest = new GetRequest("first-index", "_doc", "gvarh3gBF9fSFsHNuO49");
RequestOptions.Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
requestOptions.addHeader("Authorization", auth);
GetResponse getResponse = client.get(getRequest, requestOptions.build());
if (getResponse.isExists()) {
String sourceAsString = getResponse.getSourceAsString();
System.out.println(sourceAsString);
} else {
System.out.println("no string!");
}
}
}
Pay attention to the above implementation, there are the following important knowledge points
Authentication
The Basic Auth method is used for identity verification, which is simply to add a
key = Authorization
value = "Basic " + base64(user + ":" + pwd)
access pose
The above is an example of querying documents according to id
, which can be easily understood as three steps
- Created:
XxRequest
- Add request header:
RequestOptions.Builder.addHeader
- Execute:
client.get(xxRequest, RequestOptions)
III. Source code and related knowledge points that cannot be missed
0. Project
- Engineering: https://github.com/liuyueyi/spring-boot-demo
- Source: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/142-search-es
1. WeChat public account: Yihuihui Blog
It is not as good as a letter. The above content is purely from the family. Due to limited personal ability, it is inevitable that there will be omissions and mistakes. If you find bugs or have better suggestions, you are welcome to criticize and correct them.
The following is a gray personal blog, recording all blog posts in study and work, welcome everyone to visit
- A gray blog Personal blog https://blog.hhui.top
- A Gray Blog-Spring Special Blog http://spring.hhui.top
hhui
SpringBoot系列之数据库初始化-datasource配置方式
小灰灰Blog阅读 658
Spring Aop 动态代理
KerryWu赞 5阅读 8.7k评论 1
SpringBoot集成LibreOffice+jodconverter做文件预览(office转pdf)
Zeran赞 2阅读 6.2k
Spring系列-实战篇(5)-数据库的事务和锁
KerryWu赞 2阅读 6k评论 1
之前很火给女朋友推送微信服务号消息是怎么做的?
Java3y赞 3阅读 1.2k
简单使用spring cloud 服务注册做一个请求转发中心
weiweiyi赞 2阅读 749
消息推送平台终于要上线啦!
Java3y赞 3阅读 1k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。