1. 创建一个Springboot工程并加入相关的依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</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>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>
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;
}
}
3 对es进行相关操作
添加实体类(为批量添加准备)
@Data @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private String id; private String name; private Integer age; }
3.1 操作索引---创建索引
@Autowired
public RestHighLevelClient client;
//创建索引
@Test
void contextLoads() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("dyt_index");
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
//查看是否创建成功
System.out.println(createIndexResponse.isAcknowledged());
client.close();
}
3.2 判断索引是否存在
//判断索引是否存在
@Test
void test01() throws Exception{
GetIndexRequest getIndexRequest=new GetIndexRequest("dyt_index");
boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
3.3 删除索引
//删除索引
@Test
void test02() throws Exception{
DeleteIndexRequest deleteIndexRequest=new DeleteIndexRequest("dyt_index");
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
boolean acknowledged = delete.isAcknowledged();
System.out.println(acknowledged);
}
3.4 添加文档
//文档添加
@Test
void test03()throws Exception{
IndexRequest indexRequest=new IndexRequest("dyt_index");
indexRequest.id("1");
User user=new User("1","dyt",18);
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.status());
client.close();
}
3.5 获取文档信息
//获取文档信息
@Test
void test04() throws Exception{
GetRequest getRequest=new GetRequest("dyt_index","1");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
}
3.6 修改文档信息
//修改文档信息
@Test
void test05()throws Exception{
UpdateRequest updateRequest=new UpdateRequest("dyt_index","1");
User user=new User("1","dyt",18);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());
client.close();
}
3.7 批量添加数据
@Test
void test07()throws Exception {
BulkRequest bulk = new BulkRequest("dyt_index");
List<User> list = new ArrayList<>();
list.add(new User("2", "张三", 22));
list.add(new User("3", "李四", 23));
list.add(new User("4", "王二", 24));
list.add(new User("5", "王五", 22));
list.stream().forEach(item->bulk.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);
bulk.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulk,RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());
}
3.8 文档查询
//文档查询
@Test
void test08()throws Exception{
//
SearchRequest searchRequest=new SearchRequest("dyt_index");
//创建一个条件对象
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
TermQueryBuilder matchQuery = QueryBuilders.termQuery("name", "张");
sourceBuilder.query(matchQuery);
//分页
sourceBuilder.from(0);
sourceBuilder.size(1);
//排序
sourceBuilder.sort("age");
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("<font color='red'>");
highlightBuilder.postTags("</font>");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
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()));
Arrays.stream(hits).forEach(item-> System.out.println(item.getHighlightFields()));
}