赞
踩
话不多说,直接开冲,大家准备好~
创建索引
测试用例
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
- @Test
- void contextLoads() throws IOException {
- //1、创建索引请求
- CreateIndexRequest request = new CreateIndexRequest("es_index_test01");
- //2、客户端执行请求,请求后获得响应
- CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create (request, RequestOptions.DEFAULT);
- System.out.println(createIndexResponse);
-
- }
控制台:
查看ES的head可视化工具
刚刚创建的索引,创建成功!
一般常用这个方法,判断某个索引是否存在,然后再进行相关的查询操作
方法
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- @Test
- void testIndexExist() throws IOException {
- //创建获取索引的请求
- GetIndexRequest getIndexRequest = new GetIndexRequest("es_index_test01");
- //调用exists方法,判断这个索引是否存在
- boolean exists = restHighLevelClient.indices().exists(getIndexRequest,RequestOptions.DEFAULT);
- System.out.println(exists);
- }
控制台
方法:
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- @Test
- void testDeleteIndex() throws IOException {
- //创建删除索引的请求
- DeleteIndexRequest deleteRequest = new DeleteIndexRequest("es_index_test01");
- //调用delete方法删除索引,返回的参数是一个AcknowledgedResponse类型,可以获取到删除是否成功的布尔值
- AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteRequest, RequestOptions.DEFAULT);
- //获取是否删除成功的结果
- System.out.println(delete.isAcknowledged());
- }
-
- }
控制台:
说明该索引删除成功
1、SpringBoot中添加文档的方法
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- /**
- * @Author xgh
- * @Description ES操作文档
- * @Date 2023/7/13 20:47
- * @Return
- **/
- @Test
- void testOpeDoc() throws IOException {
- //创建对象
- User user = new User("张三",12,"1999-12-13");
- //创建请求(参数中放之前创建好的索引)
- IndexRequest request = new IndexRequest("test01");
-
- //设置规则(之前在Kiabana中通过PUT命令设置的相关的规则)
- //设置文档id
- request.id("05");
- //设置请求的超时时间
- request.timeout("1S");
-
- //将数据放到请求中
- request.source(JSON.toJSONString(user), XContentType.JSON);
-
- //客户端发送请求获取响应的结果
- IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
-
- //查看响应的结果
- System.out.println("响应的结果" + indexResponse.toString());
- System.out.println(indexResponse.status());//查看响应的状态,对应我们之前命令返回的result是create或者是update
-
- }
2、观察控制台和head可视化界面
Head可视化界面中的数据:
添加成功
1、获取文档的方法
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- /**
- * @Author xgh
- * @Description 获取文档信息
- * @Date 2023/7/13 21:04
- * @Return
- **/
- @Test
- void testGetDoc() throws IOException {
- //创建Get请求,类似于命令中的GET/index/id
- GetRequest getRequest = new GetRequest("test01", "01");
-
- //调用GET方法,发起请求,返回响应的结果
- GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
- //GetResponse对象中可以有很多种的方法,获取响应结果的不同数据
- System.out.println(documentFields.getSourceAsString()); //打印文档的内容
- System.out.println(documentFields.getId()); //获取文档的Id
- System.out.println(documentFields.getIndex()); //获取该文档的索引
- System.out.println(documentFields); //获取返回的全部内容
-
- }
2、控制台
1、方法
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- /**
- * @Author xgh
- * @Description 判断文档是否存在
- * @Date 2023/7/13 21:04
- * @Return
- **/
- @Test
- void testGetDoc() throws IOException {
- //创建Get请求,类似于命令中的GET/index/id
- GetRequest getRequest = new GetRequest("test01", "01");
- //不返回_source的上下文
- getRequest.fetchSourceContext(new FetchSourceContext(false));
- getRequest.storedFields("_none_");
-
- boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
- System.out.println(exists);
-
- }
2、控制台
文档存在就会返回一个true
1、命令
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- /**
- * @Author xgh
- * @Description 更新文档
- * @Date 2023/7/13 21:50
- * @Return
- **/
- @Test
- void updateDoc() throws IOException {
- //创建更新请求
- UpdateRequest updateRequest = new UpdateRequest("test01", "01");
-
- //设置请求最大的超时时间
- updateRequest.timeout("1s");
-
- //创建要更新的对象(对象中的属性要和索引表中的字段一致)
- User user = new User("张翠山", 56, "1779-12-03");
-
- //将更新的对象封装到请求中
- updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
-
- //客户端来执行这个请求
- UpdateResponse updateResponse = restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
- System.out.println(updateResponse.status()); //查看更新的状态,如果返回的状态是OK,那么就是更新成功
- }
2、控制台
3、查看可视化工具
更新成功!
1、方法
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
-
- /**
- * @Author xgh
- * @Description 删除文档
- * @Date 2023/7/13 22:33
- * @Return
- **/
- @Test
- void deleteDoc() throws IOException {
- //创建删除的请求
- DeleteRequest deleteRequest = new DeleteRequest("test01","02");
-
- //设置请求的超时时间
- deleteRequest.timeout("1s");
-
- //客户端发送删除的请求
- DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
- System.out.println(deleteResponse);
- }
2、控制台
3、可视化界面
02被删除了!
1、方法
- @SpringBootTest
- class EsApiStudyApplicationTests {
-
- @Autowired
- private RestHighLevelClient restHighLevelClient;
-
- /**
- * @Author xgh
- * @Description 查询文档
- * @Date 2023/7/18 20:34
- * @Return
- **/
- @Test
- void searchDoc() throws IOException {
- //创建查询请求
- SearchRequest searchRequest = new SearchRequest("test01");
-
- //构建搜索条件(建造者模式,通过建造者模式,可以构造很多的条件)
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
-
- //通过QueryBuilders工具类来构建查询条件(现在是调用的精确匹配)
- //QueryBuilders.matchAllQuery() 匹配全部数据
- TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangzhang");
- //调用query方法,查询
- searchSourceBuilder.query(termQueryBuilder);
- //构建分页
- searchSourceBuilder.from(0);
- searchSourceBuilder.size(3);
- //设置查询超时时间(60秒)
- searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
-
- //封装搜索
- searchRequest.source(searchSourceBuilder);
-
- //客户端调用search查询
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- //获取查询的数据
- SearchHits hits = searchResponse.getHits();
- System.out.println("精确查询的结果:"+JSON.toJSONString(hits));
-
- //将查询的结果以MAp的形式展示
- System.out.println("========================");
- for (SearchHit hit : searchResponse.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
2、控制台
1、方法
- /**
- * @Author xgh
- * @Description 批量操作文档
- * @Date 2023/7/18 20:13
- * @Return
- **/
- @Test
- void opeDocMore() throws IOException {
- //创建批量请求
- BulkRequest bulkRequest = new BulkRequest();
-
- //是指请求超时时间
- bulkRequest.timeout("10s");
-
- //创建测试数据,创建数组
- List<User> userList = new ArrayList<>();
- userList.add(new User("小明",24,"2026-07-05"));
- userList.add(new User("小黄",21,"2026-055-14"));
- userList.add(new User("小老板",33,"2026-02-26"));
- userList.add(new User("小千禧",45,"2026-03-12"));
-
- //批处理请求
- for (int i = 0; i < userList.size(); i++) {
- //这里调add方法,更或者删除的操作只是调用不同的方法
- bulkRequest.add(new IndexRequest("jd_goods")
- .id("" + (i+1))
- .source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
- }
-
- //执行批量请求
- BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
-
-
- //查看是否执行成功(这里是成功返false,失败返回true)
- System.out.println("是否执行成功:" + bulkResponse.hasFailures());
- }
2、控制台
3、可视化界面
至此,关于ES集成到Spring中常见的操作方法已经介绍完毕,内容比较多,建议收藏反复学习。
后续还会持续更新,希望大家可以继续关注!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。