其他分享
首页 > 其他分享> > SpringBoot整合

SpringBoot整合

作者:互联网

SpringBoot整合

1、创建工程

目录结构

目录结构

2、导入依赖

注意依赖版本和安装的版本一致

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <!-- 统一版本 -->
  4. <elasticsearch.version>7.6.1</elasticsearch.version>
  5. </properties>

导入elasticsearch

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>

提前导入fastjson、lombok

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.70</version>
  5. </dependency>
  6. <!-- lombok需要安装插件 -->
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. <optional>true</optional>
  11. </dependency>

3、创建并编写配置类

  1. @Configuration
  2. public class ElasticSearchConfig {
  3. // 注册 rest高级客户端
  4. @Bean
  5. public RestHighLevelClient restHighLevelClient(){
  6. RestHighLevelClient client = new RestHighLevelClient(
  7. RestClient.builder(
  8. new HttpHost("127.0.0.1",9200,"http")
  9. )
  10. );
  11. return client;
  12. }
  13. }

4、创建并编写实体类

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class User implements Serializable {
  5. private static final long serialVersionUID = -3843548915035470817L;
  6. private String name;
  7. private Integer age;
  8. }

5、测试

所有测试均在 SpringbootElasticsearchApplicationTests中编写

注入 RestHighLevelClient

  1. @Autowired
  2. public RestHighLevelClient restHighLevelClient;

索引的操作

1、索引的创建
  1. // 测试索引的创建, Request PUT liuyou_index
  2. @Test
  3. public void testCreateIndex() throws IOException {
  4. CreateIndexRequest request = new CreateIndexRequest("liuyou_index");
  5. CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
  6. System.out.println(response.isAcknowledged());// 查看是否创建成功
  7. System.out.println(response);// 查看返回对象
  8. restHighLevelClient.close();
  9. }
2、索引的获取,并判断其是否存在
  1. // 测试获取索引,并判断其是否存在
  2. @Test
  3. public void testIndexIsExists() throws IOException {
  4. GetIndexRequest request = new GetIndexRequest("index");
  5. boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
  6. System.out.println(exists);// 索引是否存在
  7. restHighLevelClient.close();
  8. }
3、索引的删除
  1. // 测试索引删除
  2. @Test
  3. public void testDeleteIndex() throws IOException {
  4. DeleteIndexRequest request = new DeleteIndexRequest("liuyou_index");
  5. AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
  6. System.out.println(response.isAcknowledged());// 是否删除成功
  7. restHighLevelClient.close();
  8. }

文档的操作

1、文档的添加
  1. // 测试添加文档(先创建一个User实体类,添加fastjson依赖)
  2. @Test
  3. public void testAddDocument() throws IOException {
  4. // 创建一个User对象
  5. User liuyou = new User("liuyou", 18);
  6. // 创建请求
  7. IndexRequest request = new IndexRequest("liuyou_index");
  8. // 制定规则 PUT /liuyou_index/_doc/1
  9. request.id("1");// 设置文档ID
  10. request.timeout(TimeValue.timeValueMillis(1000));// request.timeout("1s")
  11. // 将我们的数据放入请求中
  12. request.source(JSON.toJSONString(liuyou), XContentType.JSON);
  13. // 客户端发送请求,获取响应的结果
  14. IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
  15. System.out.println(response.status());// 获取建立索引的状态信息 CREATED
  16. System.out.println(response);// 查看返回内容 IndexResponse[index=liuyou_index,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
  17. }
2、文档信息的获取
  1. // 测试获得文档信息
  2. @Test
  3. public void testGetDocument() throws IOException {
  4. GetRequest request = new GetRequest("liuyou_index","1");
  5. GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
  6. System.out.println(response.getSourceAsString());// 打印文档内容
  7. System.out.println(request);// 返回的全部内容和命令是一样的
  8. restHighLevelClient.close();
  9. }
3、文档的获取,并判断其是否存在
  1. // 获取文档,判断是否存在 get /liuyou_index/_doc/1
  2. @Test
  3. public void testDocumentIsExists() throws IOException {
  4. GetRequest request = new GetRequest("liuyou_index", "1");
  5. // 不获取返回的 _source的上下文了
  6. request.fetchSourceContext(new FetchSourceContext(false));
  7. request.storedFields("_none_");
  8. boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
  9. System.out.println(exists);
  10. }
4、文档的更新
  1. // 测试更新文档内容
  2. @Test
  3. public void testUpdateDocument() throws IOException {
  4. UpdateRequest request = new UpdateRequest("liuyou_index", "1");
  5. User user = new User("lmk",11);
  6. request.doc(JSON.toJSONString(user),XContentType.JSON);
  7. UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
  8. System.out.println(response.status()); // OK
  9. restHighLevelClient.close();
  10. }
5、文档的删除
  1. // 测试删除文档
  2. @Test
  3. public void testDeleteDocument() throws IOException {
  4. DeleteRequest request = new DeleteRequest("liuyou_index", "1");
  5. request.timeout("1s");
  6. DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
  7. System.out.println(response.status());// OK
  8. }
6、文档的查询
  1. // 查询
  2. // SearchRequest 搜索请求
  3. // SearchSourceBuilder 条件构造
  4. // HighlightBuilder 高亮
  5. // TermQueryBuilder 精确查询
  6. // MatchAllQueryBuilder
  7. // xxxQueryBuilder ...
  8. @Test
  9. public void testSearch() throws IOException {
  10. // 1.创建查询请求对象
  11. SearchRequest searchRequest = new SearchRequest();
  12. // 2.构建搜索条件
  13. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  14. // (1)查询条件 使用QueryBuilders工具类创建
  15. // 精确查询
  16. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "liuyou");
  17. // // 匹配查询
  18. // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
  19. // (2)其他<可有可无>:(可以参考 SearchSourceBuilder 的字段部分)
  20. // 设置高亮
  21. searchSourceBuilder.highlighter(new HighlightBuilder());
  22. // // 分页
  23. // searchSourceBuilder.from();
  24. // searchSourceBuilder.size();
  25. searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
  26. // (3)条件投入
  27. searchSourceBuilder.query(termQueryBuilder);
  28. // 3.添加条件到请求
  29. searchRequest.source(searchSourceBuilder);
  30. // 4.客户端查询请求
  31. SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
  32. // 5.查看返回结果
  33. SearchHits hits = search.getHits();
  34. System.out.println(JSON.toJSONString(hits));
  35. System.out.println("=======================");
  36. for (SearchHit documentFields : hits.getHits()) {
  37. System.out.println(documentFields.getSourceAsMap());
  38. }
  39. }
前面的操作都无法批量添加数据
  1. // 上面的这些api无法批量增加数据(只会保留最后一个source)
  2. @Test
  3. public void test() throws IOException {
  4. IndexRequest request = new IndexRequest("bulk");// 没有id会自动生成一个随机ID
  5. request.source(JSON.toJSONString(new User("liu",1)),XContentType.JSON);
  6. request.source(JSON.toJSONString(new User("min",2)),XContentType.JSON);
  7. request.source(JSON.toJSONString(new User("kai",3)),XContentType.JSON);
  8. IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
  9. System.out.println(index.status());// created
  10. }
7、批量添加数据
  1. // 特殊的,真的项目一般会 批量插入数据
  2. @Test
  3. public void testBulk() throws IOException {
  4. BulkRequest bulkRequest = new BulkRequest();
  5. bulkRequest.timeout("10s");
  6. ArrayList<User> users = new ArrayList<>();
  7. users.add(new User("liuyou-1",1));
  8. users.add(new User("liuyou-2",2));
  9. users.add(new User("liuyou-3",3));
  10. users.add(new User("liuyou-4",4));
  11. users.add(new User("liuyou-5",5));
  12. users.add(new User("liuyou-6",6));
  13. // 批量请求处理
  14. for (int i = 0; i < users.size(); i++) {
  15. bulkRequest.add(
  16. // 这里是数据信息
  17. new IndexRequest("bulk")
  18. .id(""+(i + 1)) // 没有设置id 会自定生成一个随机id
  19. .source(JSON.toJSONString(users.get(i)),XContentType.JSON)
  20. );
  21. }
  22. BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
  23. System.out.println(bulk.status());// ok
  24. }
 

标签:index,SpringBoot,request,System,liuyou,整合,new,restHighLevelClient
来源: https://www.cnblogs.com/flyer-ovo/p/16061957.html