springboot 2.2.5 整合elasticsearch
2020-07-09 本文已影响0人
天不生我小金
前言:该博客主要是记录自己学习的过程,方便以后查看,当然也希望能够帮到大家。
开整吧。完整代码地址在结尾!!
第一步,在pom.xml加入依赖,如下
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
此处有坑,springboot 2.2.5 版本默认 elasticsearch 版本为 6.8.6 ,根据需要自己修改版本,此处我修改为 7.7.0版本(因为我的 elasticsearch 版本为 7.7.0),如下
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.7.0</elasticsearch.version>
</properties>
第二步,创建 ElasticSearchClientConfig 配置文件,如下
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description:
* @Author: jinhaoxun
* @Date: 2020/7/8 4:23 下午
* @Version: 1.0.0
*/
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("172.16.31.191", 9200, "http"),
new HttpHost("172.16.31.192", 9200, "http"),
new HttpHost("172.16.31.193", 9200, "http")
));
return restHighLevelClient;
}
}
第三步,创建简单操作实体 User,如下
import lombok.Getter;
import lombok.Setter;
/**
* @Description:
* @Author: jinhaoxun
* @Date: 2020/7/8 4:57 下午
* @Version: 1.0.0
*/
@Setter
@Getter
public class User {
private String name;
private int age;
}
第四步,编写单元测试类,ElasticsearchApplicationTests,并进行测试,如下
import com.alibaba.fastjson.JSON;
import com.jinhaoxun.elasticsearch.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@Slf4j
// 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
@SpringBootTest
class ElasticsearchApplicationTests {
@Resource
private RestHighLevelClient restHighLevelClient;
@Test
void testCreateIndex() throws Exception {
// 创建索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test_index");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
@Test
void testExistIndex() throws Exception {
// 判断索引是否存在
GetIndexRequest getIndexRequest = new GetIndexRequest("test_index");
boolean exist = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exist);
}
@Test
void testDeleteIndex() throws Exception {
// 判断索引是否存在
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test_index");
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(acknowledgedResponse.isAcknowledged());
}
@Test
void testAddDocument() throws Exception {
// 新增文档
User user = new User();
user.setAge(12);
user.setName("落雨");
IndexRequest indexRequest = new IndexRequest("test_index");
// 设置超时时间
indexRequest.id("1");
indexRequest.timeout(TimeValue.timeValueSeconds(1));
// 转换为json字符串
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
}
@Test
void testIsExistsDocument() throws Exception {
// 判断是否存在文档
GetRequest getRequest = new GetRequest("test_index", "1");
// 不获取返回的_source的上下文
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void testGetDocument() throws Exception {
// 获取文档
GetRequest getRequest = new GetRequest("test_index", "1");
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
}
@Test
void testUpdateDocument() throws Exception {
// 更新文档
User user = new User();
user.setAge(22);
user.setName("落雨");
UpdateRequest updateRequest = new UpdateRequest("test_index", "1");
updateRequest.timeout(TimeValue.timeValueSeconds(1));
updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
@Test
void testDeleteDocument() throws Exception {
// 删除文档
DeleteRequest deleteRequest = new DeleteRequest("test_index", "1");
deleteRequest.timeout(TimeValue.timeValueSeconds(1));
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
@Test
void testBulkRequest() throws Exception {
// 批量插入
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout(TimeValue.timeValueSeconds(1));
for (int i = 0; i < 10 ; i++){
User user = new User();
user.setAge(10 + i);
user.setName("落雨" + i);
bulkRequest.add(
new IndexRequest("test_index")
.id("" + (i + 100))
.source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse bulkItemResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkItemResponse.hasFailures());
}
@Test
void testSearchRequest() throws Exception {
// 搜索请求
SearchRequest searchRequest = new SearchRequest("test_index");
// 条件构造
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构造高亮
searchSourceBuilder.highlighter();
// 精确查询
// QueryBuilders.termQuery();
// 匹配所有
// QueryBuilders.matchAllQuery();
// 精确查询
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "落雨");
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
searchResponse.getHits().forEach(x -> {
System.out.println(x.getSourceAsMap());
});
}
@BeforeEach
void testBefore(){
log.info("测试开始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
@AfterEach
void testAfter(){
log.info("测试结束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
完整代码地址:https://github.com/luoyusoft/springboot-demo
注:此工程包含多个module,本文所用代码均在elasticsearch-demo模块下
后记:本次分享到此结束,本人水平有限,难免有错误或遗漏之处,望大家指正和谅解,欢迎评论留言。