SpringBoot 操作elasticsearch
版本环境
- jdk1.8
- elasticsearch 7.6.1
maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
注意版本,如果版本不对 需要手动进行版本指定
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
创建配置类
package com.oss.oss.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;
/**
* 声明其为一个配置对象
* 生成一个连接elasticsearch 的连接对象 注入到spring容器中
*/
@Configuration
public class ElasticsearchClientConfiguration {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
}
创建测试方法
api 说明文档地址
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-exists.html
package com.oss.oss;
import com.alibaba.fastjson.JSON;
import com.oss.oss.bean.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.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.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class OssApplicationTests {
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//测试创建索引
@Test
public void createIndex(){
try {
CreateIndexRequest request = new CreateIndexRequest("test04");
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断索引是否存在
*/
@Test
public void existsIndex(){
try {
GetIndexRequest request = new GetIndexRequest("test04");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
@Test
public void deleteIndex(){
try {
DeleteIndexRequest request = new DeleteIndexRequest("test04");
AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(deleteIndexResponse.isAcknowledged());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文档新增
*/
@Test
public void createDocument(){
try{
IndexRequest request = new IndexRequest("test03");
User User = new User("李白","诗仙李白",56);
request.id("4");
request.timeout("1s");
request.source(JSON.toJSONString(User), XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
} catch (Exception e){
e.printStackTrace();
}
}
/**
* 文档是否存在
*/
@Test
public void existDocument(){
try {
GetRequest getRequest = new GetRequest("test03","4");
getRequest.fetchSourceContext(new FetchSourceContext(false));
getRequest.storedFields("_none_");
boolean exis = client.exists(getRequest,RequestOptions.DEFAULT);
System.out.println(exis);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 查询文档
*/
@Test
public void searchDocument(){
try {
GetRequest getRequest = new GetRequest("test03","4");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
System.out.println(getResponse);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 条件查询
*
*/
@Test
public void mulitSearchDocument(){
try {
SearchRequest searchRequest = new SearchRequest("test03");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("name","李白"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("=================================");
for (SearchHit documentFields : searchResponse.getHits().getHits()){
System.out.println(documentFields.getSourceAsMap());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}