Elasticsearch6.8.4 RestHighLevelClient分页高亮查询实例
原创    loonbs    发表于:2019-12-11 09:52:57
  阅读 :1713   收藏   编辑

创建RestHighLevelClient bean

@Configuration
public class EsConf {

    @Bean
    RestHighLevelClient elasticsearchClient() {
        ClientConfiguration configuration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .build();
        RestHighLevelClient client = RestClients.create(configuration).rest();

        return client;
    }

}

搜索代码片段

@Autowired
private RestHighLevelClient esClient;
  /**
     * 
     * @param key 关键字
     * @param p 页码
     * @param pageSize 页大小
     * @throws Exception
     */
    public void search(String key, Integer p, Integer pageSize) throws  Exception{
        log.info("searching key is :" + key);

        //init
        if(pageSize == null)  pageSize = 20;
        if(p == null ) p = 1;
        Integer from = (p - 1) * pageSize;
        if(from <= 0) from = 0;
    
        //创建queryBuilder, 从es字段:title,desc查询
        QueryBuilder builder =  QueryBuilders.multiMatchQuery(key,"title","desc");
        
        //设置需要高亮的es字段
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.field("desc");

        //设置es索引:products 
        SearchRequest searchRequest = new SearchRequest("products");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(builder);
        sourceBuilder.from(from);
        sourceBuilder.size(pageSize);
        sourceBuilder.highlighter(highlightBuilder);
        sourceBuilder.timeout(new TimeValue(1000));
        searchRequest.source(sourceBuilder);
        
        //搜索
        SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();
        long total = searchHits.getTotalHits();
        
        log.info("总记录数:"  + total);
        log.info("总页码数:" + (total % pageSize == 0 ? total / pageSize : (total / pageSize + 1 )) );
        log.info("当前页数:" + p);     
        
        SearchHit[] hits = searchHits.getHits();

        for (SearchHit hit : hits) {

            Map source = hit.getSourceAsMap();
            
            log.info("文档ID:" + hit.getId());
            log.info("文档得分:" + hit.getScore());
            
            //首先从highlight获取es字段,如果命中关键字则有,没有命中直接从Source获取
            if(hit.getHighlightFields().get("title") != null){
                Text[] titleTexts  =  hit.getHighlightFields().get("title").getFragments();
                String title = "";
                for(Text text : titleTexts){
                    title += text;
                }
                log.info("文档字段title:" + title);
            }else{
                log.info("文档字段title:" + source.get("title"));
            }
            
            //获取es字段desc
            if(hit.getHighlightFields().get("desc") != null){
                Text[] titleTexts  =  hit.getHighlightFields().get("desc").getFragments();
                String desc = "";
                for(Text text : titleTexts){
                    desc += text;
                }
                log.info("文档字段desc:" + desc);
            }else{
                log.info("文档字段desc:" + source.get("desc"));
            }


           
        }
      
    }


评论
条评论

推荐阅读