(1)创建一个Springboot工程并加入相关的依赖
com.alibaba
fastjson
1.2.75
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
(2) 创建一个配置,获取ES工具类对象
@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 contextLoads() throws Exception{
//该类把创建索引的信息封装到该类中
CreateIndexRequest createIndexRequest = new CreateIndexRequest("qy151-index");
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isAcknowledged());
}
@Test
public void testDeleteIndex() throws Exception {
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy151-index");
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
@Test
public void testIndexExists() throws Exception{
GetIndexRequest getIndexRequest=new GetIndexRequest("qy151-index");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
创建一个实体类User
添加文档
//添加文档--PUT /索引/1 {name:"",age:"",address:""}
@Test
public void testInsertDoc() throws Exception{
IndexRequest indexRequest=new IndexRequest("qy151-index");
indexRequest.id("1");//指定文档的id
//指定文档的内容:String文档的json内容,XContentType xContentType:以什么格式
indexRequest.source(JSON.toJSONString(new User("张三","北京",22)), XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(indexResponse.getResult());
}
//获取文档 GET /索引/_doc/1
@Test
public void testGetDoc() throws Exception{
GetRequest indexRequest=new GetRequest("qy151-index");
indexRequest.id("1");
GetResponse getResponse = client.get(indexRequest, RequestOptions.DEFAULT);
String string = getResponse.getSourceAsString();
User user = JSON.parseObject(string, User.class);
Map map = getResponse.getSourceAsMap();
System.out.println(map.get("address"));
}
//判断索引文档是否存在
@Test
public void testDocExist() throws Exception{
GetRequest indexRequest=new GetRequest("qy151-index");
indexRequest.id("2");
boolean exists = client.exists(indexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
//文档操作---删除文档
@Test
public void testDeleteDoc() throws Exception{
DeleteRequest deleteRequest=new DeleteRequest("qy151-index");
deleteRequest.id("1");
DeleteResponse delete = client.delete(deleteRequest,RequestOptions.DEFAULT);
System.out.println(delete.getResult());
}
//修改文档
//POST /qy151-index/1
//{ "doc":{
// "name":"刘德华"
// }
// }
@Test
public void testUpdate() throws Exception{
UpdateRequest updateRequest = new UpdateRequest("qy151-index","1");
User user = new User();
user.setName("刘德华");
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getResult());
}
//批量添加文档
@Test
public void testBuck() throws Exception{
BulkRequest bulkRequest = new BulkRequest("qy151-index");
List list = new ArrayList<>();
list.add(new User("4","喜羊羊","羊村",15));
list.add(new User("5","美羊羊","羊村",15));
list.add(new User("6","懒羊羊","羊村",15));
list.add(new User("7","沸羊羊","羊村",16));
list.add(new User("8","暖羊羊","羊村",16));
list.stream().forEach(item->bulkRequest.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
/* for(User user:list){
IndexRequest indexRequest=new IndexRequest();
indexRequest.id(user.getId());
indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
bulkRequest.add(indexRequest);
}*/
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.hasFailures());
}
//复杂查询
//搜索查询---GET /索引/_search
// {
// "query":{
// "":{}
// },
// "from":
// "size":
// "_source":["",""],
// "sort":{}
// }
//1. 搜索请求对象SearchRequest
//2. 构建一个条件对象SearchSourceBuilder
//3. 把条件对象放入搜索请求对象中
//4. 执行搜索功能
@Test
public void testSearch() throws Exception{
SearchRequest searchRequest = new SearchRequest();
//创建一个条件对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//条件对象的条件
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("name","羊");
searchSourceBuilder.query(matchQueryBuilder);
//分页查询 从第零条开始 每页显示2条
searchSourceBuilder.from(0);
searchSourceBuilder.size(2);
//排序
/*searchSourceBuilder.sort("age");*/
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("");
highlightBuilder.postTags("");
searchSourceBuilder.highlighter(highlightBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数:"+searchResponse.getHits().getTotalHits().value);
SearchHit[] hits = searchResponse.getHits().getHits();
/*Arrays.stream(hits).forEach(item-> System.out.println(item.getSourceAsString()));*/
for (SearchHit array:hits
) {
System.out.println(array.getSourceAsString());
}
Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
/*for (SearchHit aaa:hits
) {
System.out.println(aaa.getHighlightFields());
}*/
}