一、创建springBoot项目
1、创建一个空项目
2、输入项目名称
3、修改版本号以及elasticsearch的版本号
4、更改版本号
5、xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.songmo</groupId>
<artifactId>songmo-es-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>songmo-es-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!--自定义版本依赖-->
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.7.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
6、elasticsearch的config文件配置
package com.songmo.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;
/**
* @author songmo
* @Description: elasticsearch 配置文件
* @date 2020/7/6 16:42
*/
@Configuration
public class ElasticSearchConfig {
//restHighLevelClient 相当于spring中的id RestHighLevelClient相当于class
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
二、具体的API使用
1、创建索引
@Autowired
private RestHighLevelClient restHighLevelClient;
//创建索引
@Test
void testCreatIndex() throws IOException {
//创建索引请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest("songmo");
//创建执行请求
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
2、获取索引
//获取索引
@Test
void testGetIndex() throws IOException {
GetIndexRequest getIndexRequest =new GetIndexRequest("songmo");
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
3、删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("songmo");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
4、添加文档信息
//文档添加信息
@Test
void testDocument() throws IOException {
User user = new User("test1", 10);
IndexRequest indexRequest = new IndexRequest("songmo");
indexRequest.id("1");
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.timeout("1s");
IndexRequest source = indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
//发送请求
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
}
5、获取文档信息
//获取文档
@Test
void testGetDocument() throws IOException {
GetRequest getRequest = new GetRequest("songmo", "1");
//不获取 _souce上下文
// getRequest.fetchSourceContext(new FetchSourceContext(false));
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
if(exists){
//获取文档信息
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
System.out.println(documentFields);
}
}
6、更新文档信息
//更新文档信息
@Test
void testUpdateDocument() throws IOException {
UpdateRequest updateRequest =new UpdateRequest("songmo","1");
updateRequest.timeout("1s");
User user = new User("白仙子", 18);
UpdateRequest doc = updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
}
7、删除文档信息
//删除文档
@Test
void testDelectDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("songmo", "1");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse);
}
8、批量插入
//批量插入数据
@Test
void testInsertListDocument() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
List<User> list =new ArrayList<User>();
list.add(new User("白仙子",19));
list.add(new User("诸葛仙子",18));
list.add(new User("张仙子",20));
list.add(new User("刘仙子",21));
list.add(new User("李仙子",25));
for (int i = 0; i <list.size() ; i++) {
bulkRequest.add(new IndexRequest("songmo").id(""+i+1).source(JSON.toJSONString(list.get(i)),XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否失败 false 代表成功
System.out.println(bulk.hasFailures());
}
9、查询数据search
//查询文档信息
@Test
void testSearchDocumen() throws IOException {
SearchRequest searchRequest = new SearchRequest("songmo");
//构建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//QueryBuilders.termQuery 精确查找
// TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "李仙子");
//QueryBuilders.matchAllQuery查询所有
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(matchAllQueryBuilder);
//构建分页
searchSourceBuilder.from();
searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSON(search.getHits()));
for (SearchHit searchHit : search.getHits().getHits()) {
System.out.println(searchHit.getSourceAsMap());
}
}