一、导入依赖
创建一个spring boot项目
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependency>
</dependencies>
二、创建一个配置类
这个配置类是我们获取ES的工具
package com.gsh.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 ESConfig {
//该对象可以对我们的ES进行相关的操作
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
return client;
}
}
三、ES的有关操作
1.对索引的操作
对索引的操作:
es对象.indices().相关操作
(1)创建索引
@Test
//创建索引
void CreateIndex() throws Exception {
//把该类创建的索引信息封装在该类中
CreateIndexRequest createIndexRequest=new CreateIndexRequest("qy151-index");
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
(2)删除索引
@Test
//删除索引
void deleteIndex() throws Exception{
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("qy151-index");
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}
(3)判断该索引是否存在
@Test
//判断该索引是否存在
void exitsIndex() throws Exception{
GetIndexRequest getIndexRequest=new GetIndexRequest("qy151-index");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
2.对文档的相关操作
对文档的操作:
es对象.直接操作
(1)添加一个文档
@Test
//添加文档
void addWendang() throws Exception {
IndexRequest indexRequest = new IndexRequest("qy151-index");
indexRequest.id("2");//指定添加文档的id不指定随机生成
//指定文档的内容:String文档的json内容,XContentType :以什么格式
System.out.println(indexRequest.source(JSON.toJSONString(new User("张三", 24, "郑州")), XContentType.JSON));
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
}
(2)查询/获取一个文档
@Test
//获取文档
public void GetWendang ()throws Exception{
GetRequest getRequest=new GetRequest("qy151-index");
getRequest.id("1");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
//获取查询到的对象
String source = documentFields.getSourceAsString();//将获取到的结果以字符串的形式
User user = JSON.parseObject(source, User.class);
System.out.println(user);
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();//将获取到的结果以map的形式
System.out.println(sourceAsMap.get("age"));
}
(3) 查询一个文档是否存在
@Test
//判断文档是否存在
void existwndang()throws Exception{
GetRequest getRequest=new GetRequest("qy151-index");
getRequest.id("1");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
(4)删除文档
@Test
//删除文档
void deleteWendang()throws Exception{
DeleteRequest deleteRequest=new DeleteRequest("qy151-index");
deleteRequest.id("1");
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete);
}
(5)修改文档
@Test
//修改文档
void updateWendang()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());
}
(6)批量添加文档
@Test
//批量添加文档
public void buckWendang()throws Exception {
BulkRequest bulkRequest=new BulkRequest("qy151-index");
//定义一个集合用于存放批量添加的对象
List<Student> list=new ArrayList<>();
list.add(new Student("1","李四","男"));
list.add(new Student("2","王五","女"));
list.add(new Student("3","赵六","男"));
list.add(new Student("4","徐七","女"));
//使用Stream流
list.stream().
forEach(item->bulkRequest.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk);
}
(7)复杂查询
@Test
void Search()throws Exception{
SearchRequest searchRequest=new SearchRequest("qy151-index");
//创建条件对象
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
//精准查询
TermQueryBuilder matchQuery = QueryBuilders.termQuery("name","王");
searchSourceBuilder.query(matchQuery);
//分页查找
searchSourceBuilder.from(0);
searchSourceBuilder.size(1);
//排序查找,不能以名字作为条件
//searchSourceBuilder.sort("age");
//高量查询
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("<font color='blue'>");//前面添加
highlightBuilder.postTags("</font>");//后面添加
searchSourceBuilder.highlighter(highlightBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
//输出总条数
System.out.println("总条数:"+search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
Arrays.stream(hits).forEach(iteam-> System.out.println(iteam.getSourceAsString()));
//输出高量
Arrays.stream(hits).forEach(iteam-> System.out.println(iteam.getHighlightFields()));
System.out.println(search);
}