SpringBoot整合ElasticSearch
1、导入依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.0</version>
</dependency>
2、配置
<properties>
<java.version>1.8</java.version>
<!--自定义版本依赖,保证和本地一致-->
<elasticsearch.version>7.9.0</elasticsearch.version>
</properties>
3、ES配置类
package com.zym.test.config;
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;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost",9200,"http")
)
);
return client;
}
}
4、测试
package com.zym.test;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class SpringbootEsApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//索引的操作
//创建索引
@Test
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("test_index");
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
}
//获取索引
@Test
public void getIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("test_index");
boolean exists = restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
System.out.println(exists);
}
//删除索引
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test_index");
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
//添加文档
@Test
public void addDocument() throws IOException {
User user = new User("zym",3);
IndexRequest index = new IndexRequest("test_index");
index.id("1");
index.timeout(TimeValue.MINUS_ONE);
index.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(index, RequestOptions.DEFAULT);
System.out.println(response.status());
}
//获取文档
@Test
public void isExistsDocument() throws IOException {
GetRequest request = new GetRequest("test_index","1");
//不获取返回的_source上下文
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//获取信息
@Test
public void getDocument() throws IOException {
GetRequest request = new GetRequest("test_index","1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println(response);
}
//更新
@Test
public void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("test_index","1");
request.timeout("1s");
User user = new User("张三", 24);
request.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response);
}
//删除
@Test
public void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("test_index","1");
request.timeout("1s");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
//批量插入
@Test
public void batchAddDocument() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
List<User> userList = new ArrayList<User>();
userList.add(new User("zym",1));
userList.add(new User("zym",2));
userList.add(new User("zym",3));
userList.add(new User("zym",4));
userList.add(new User("zym",5));
for (int i = 0; i < userList.size(); i++) {
bulkRequest.add(new IndexRequest("test_index").id(""+(i+1)).source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
}
BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(responses.status());
}
//查询
@Test
public void search() throws IOException {
SearchRequest searchRequest = new SearchRequest("test_index");
//构建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zym");
searchSourceBuilder.query(termQueryBuilder);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response.getHits()));
}