4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.guan
springboot-es
0.0.1-SNAPSHOT
springboot-es
Demo project for Spring Boot
1.8
com.alibaba
fastjson
1.2.79
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
package com.guan.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;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-08-16 10:02:44
*/
@Configuration
public class EsConfig {
//该对象可以对我们的ES进行相关的操作
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
return client;
}
}
@Test
void testCreate() throws Exception{
//创建索引--把该类的索引信息封装到该类中
CreateIndexRequest createIndexRequest=new CreateIndexRequest("text1");
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isAcknowledged());
}
@Test
public void TestDelete()throws Exception{
//删除索引
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("text1");
//deleteIndexRequest删除索引请求
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
@Test
//判断索引是否存在
public void TestExist()throws Exception{
//获取索引的请求
GetIndexRequest getIndexRequest=new GetIndexRequest("text1");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
package com.guan.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-08-16 14:55:03
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String id;
private String name;
private String address;
private int age;
public User(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
}
@Test
public void insertDoc()throws Exception{
//indexRequest索引请求
IndexRequest indexRequest=new IndexRequest("demo1");
//指定文档添加的id
indexRequest.id("3");
//指定文档的内容 XContentType:以什么样的格式 toJSONString 转为json
indexRequest.source(JSON.toJSONString(new User("娃哈哈","西藏",18)),XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
}
@Test
public void createDoc()throws Exception{
//GetRequest获取请求
GetRequest indexRequest=new GetRequest("demo1");
//获取查询的id
indexRequest.id("3");
GetResponse documentFields = client.get(indexRequest, RequestOptions.DEFAULT);
//将查询的结果封装成map
Map map = documentFields.getSourceAsMap();
System.out.println(map);
}
//判断文档是否存在
@Test
public void exitDoc()throws Exception{
//获取索引
GetRequest getRequest=new GetRequest("demo1");
//获取文档的id
getRequest.id("3");
//判断文档是否存在
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
//文档的删除
@Test
public void deleteDoc()throws Exception{
//删除请求--根据id删除
DeleteRequest deleteRequest=new DeleteRequest("demo1");
deleteRequest.id("3");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.getResult());
}
@Test
public void updateDoc()throws Exception{
//修改请求--根据id修改
UpdateRequest updateRequest=new UpdateRequest("demo1","4");
User user=new User();
user.setName("李雷");
//将修改的文档转json
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getResult());
}
//批量添加
@Test
public void Buck()throws Exception{
//bulk批量
//批量请求
BulkRequest bulkRequest=new BulkRequest("demo1");
List list=new ArrayList<>();
list.add(new User("5","哈利","美国",18));
list.add(new User("6","百世可乐","上海",18));
list.add(new User("7","可口可乐","杭州",18));
list.add(new User("8","今麦郎","西藏",18));
//list.stream().forEach(item->bulkRequest.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
//循环
for (User user:list) {
//循环添加到bulk中
//add()里面需要IndexRequest的参数 id(user.getId()--设置id source:里面是数据
bulkRequest.add(new IndexRequest().id(user.getId()).source(JSON.toJSONString(user),XContentType.JSON));
}
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否有故障 -false是没有故障 -true有故障
System.out.println(bulk.hasFailures());
}
//复杂查询
@Test
public void TestSearch()throws Exception{
//1. 获取搜索请求对象--SearchRequest
SearchRequest searchRequest=new SearchRequest("demo1");
//2. 构建一个条件对象--SearchSourceBuilder
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//query--需要一个QueryBuilder的类型的参数
//QueryBuilders 查询构建器
MatchQueryBuilder age = QueryBuilders.matchQuery("age", "18");
searchSourceBuilder.query(age);
//3. 把条件对象放入搜索请求对象中
searchRequest.source(searchSourceBuilder);
//4. 执行搜索功能
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数"+search.getHits().getTotalHits());
SearchHit[] hits = search.getHits().getHits();
Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
}
form(从第几条开始查询)size(每页显示几条数据) 分页
//复杂查询
@Test
public void TestSearch()throws Exception{
//1. 获取搜索请求对象--SearchRequest
SearchRequest searchRequest=new SearchRequest("demo1");
//2. 构建一个条件对象--SearchSourceBuilder
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//query--需要一个QueryBuilder的类型的参数
//QueryBuilders 查询构建器 matchQuery--匹配查询
MatchQueryBuilder age = QueryBuilders.matchQuery("age", "18");
searchSourceBuilder.query(age);
//从第几个开始查询
searchSourceBuilder.from(0);
//每页显示几条
searchSourceBuilder.size(2);
//3. 把条件对象放入搜索请求对象中
searchRequest.source(searchSourceBuilder);
//4. 执行搜索功能
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数"+search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
}
//复杂查询
@Test
public void TestSearch()throws Exception{
//1. 获取搜索请求对象--SearchRequest
SearchRequest searchRequest=new SearchRequest("demo1");
//2. 构建一个条件对象--SearchSourceBuilder
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//query--需要一个QueryBuilder的类型的参数
//QueryBuilders 查询构建器 matchQuery--匹配查询
MatchQueryBuilder age = QueryBuilders.matchQuery("age", "18");
searchSourceBuilder.query(age);
//从第几个开始查询
// searchSourceBuilder.from(0);
//每页显示几条
//searchSourceBuilder.size(2);
//排序
searchSourceBuilder.sort("age");
//3. 把条件对象放入搜索请求对象中
searchRequest.source(searchSourceBuilder);
//4. 执行搜索功能
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数"+search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
}
//复杂查询
@Test
public void TestSearch()throws Exception{
//1. 获取搜索请求对象--SearchRequest
SearchRequest searchRequest=new SearchRequest("demo1");
//2. 构建一个条件对象--SearchSourceBuilder
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//query--需要一个QueryBuilder的类型的参数
//QueryBuilders 查询构建器 matchQuery--匹配查询
MatchQueryBuilder age = QueryBuilders.matchQuery("name", "可口可");
searchSourceBuilder.query(age);
//从第几个开始查询
// searchSourceBuilder.from(0);
//每页显示几条
//searchSourceBuilder.size(2);
//排序
// searchSourceBuilder.sort("age");
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
//highlighter--需要一个高亮对象
highlightBuilder.field("name"); //对那一列高亮
highlightBuilder.preTags(""); //高亮的前置标签
highlightBuilder.postTags(""); //高亮的后置标签
searchSourceBuilder.highlighter(highlightBuilder);
//3. 把条件对象放入搜索请求对象中
searchRequest.source(searchSourceBuilder);
//4. 执行搜索功能
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
// System.out.println("总条数"+search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
//Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));
Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
}