SpringBoot+Elasticsearch

# 以es7.8.1版本为例

  1. 导入springboot集成es相关jar包;

     <dependency>
         <groupId>org.elasticsearch.client</groupId>
         <artifactId>elasticsearch-rest-high-level-client</artifactId>
         <version>7.8.1</version>
     </dependency>
    
     <dependency>
         <groupId>org.elasticsearch</groupId>
         <artifactId>elasticsearch</artifactId>
         <version>7.8.1</version>
     </dependency>
    
     <dependency>
         <groupId>org.elasticsearch.client</groupId>
         <artifactId>elasticsearch-rest-client</artifactId>
         <version>7.8.1</version>
     </dependency>
    
  2. 编写es的简单配置类,es7.x以上版本支持http连接方式

     @Configuration
     public class ElasticSearchClientConfig {
    
     	@Bean
     	public RestHighLevelClient restHighLevelClient() {
    		 return new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.0.178", 9200, "http")));
      	}
     }
    
  3. 配置类完成后直接使用Autowired注解注入即可编写业务代码

     @Autowired
     private RestHighLevelClient restHighLevelClient;
     
     @Override
     public ResultBody findCardListApp(AppBbsCardManagementGetVo appBbsCardManagementGetVo) {
     //获取当前登录人
     OpenUserDetails user = OpenHelper.getUser();
     int totalNum = 0;
     List<Map<String, Object>> data = new ArrayList<>();
     //设置高亮样式
     HighlightBuilder highlightBuilder = new HighlightBuilder();
     highlightBuilder.preTags("<font color=‘red‘>");
     highlightBuilder.postTags("</font>");
     //是否多个字段高亮false为是
     highlightBuilder.requireFieldMatch(false);
     //条件字段
     String condition = appBbsCardManagementGetVo.getCondition();
     //构建查询对象
     BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
     SearchSourceBuilder SearchSourceBuilder = new SearchSourceBuilder()
             .highlighter(highlightBuilder.field("title"))
             .highlighter(highlightBuilder.field("content"))
             .sort("revealStatus", SortOrder.DESC)
             .sort("issueTime", SortOrder.DESC)
             .from((appBbsCardManagementGetVo.getOffset() - 1) * appBbsCardManagementGetVo.getPageSize())
             .size(appBbsCardManagementGetVo.getPageSize());
     //未删除
     boolQueryBuilder.must(QueryBuilders.termQuery("isDelete",0));
     //已发布
     boolQueryBuilder.must(QueryBuilders.termQuery("issueStatus",0));
     //启用
     boolQueryBuilder.must(QueryBuilders.termQuery("cardStatus",0));
     if (condition != null && !"".equals(condition)){
         boolQueryBuilder.must(QueryBuilders.matchQuery("title",condition));
         boolQueryBuilder.must(QueryBuilders.matchQuery("content",condition));
     }
     SearchSourceBuilder.query(boolQueryBuilder);
     SearchRequest searchRequest = new SearchRequest("bbs_card_management");
     searchRequest.source(SearchSourceBuilder);
     try {
     	//使用es客户端调用查询接口即可
         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
         SearchHits hits = search.getHits();
     	//拿到查询的数据
         SearchHit[] hits1 = hits.getHits();
         for (SearchHit documentFields : hits1) {
             Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
             HighlightField title = documentFields.getHighlightFields().get("title");
             HighlightField content = documentFields.getHighlightFields().get("content");
             if (title != null){
     	     //将高亮字段数据覆盖原数据
                 sourceAsMap.put("title",title.fragments()[0].toString());
             }
             if (content != null){
     	     //将高亮字段数据覆盖原数据
                 sourceAsMap.put("content",content.fragments()[0].toString());
             }
             //判断是否登陆
             if (user != null){
                 List<String> users = (List<String>) sourceAsMap.get("giveLikeUsers");
                 if (users.contains(user.getId())){
                     sourceAsMap.put("isMeLike",1);
                 }
             }
             data.add(sourceAsMap);
         }
         //查询的数据总条数
         totalNum = (int) hits.getTotalHits().value;
     } catch (Exception e) {
         e.printStackTrace();
     }
     Map<String, Object> result = new HashMap<>(2);
     result.put("cardList",data);
     result.put("totalNum",totalNum);
     return ResultBody.ok().data(result);
     }

SpringBoot+Elasticsearch

上一篇:linux下jdk8安装


下一篇:ios 开发之--逆序输出字符串