springboot集成es实现搜索功能

项目上有需求,需要集成es实现分词搜索,提高搜索效率。既然用到了es的人,相信也会提前了解es的功能,这里就不赘述了。前提是需要通过像logstash这种工具,将数据灌入到es中,才能通过es在java中实现快速搜索功能。

package cn.xxx.rdc.knowledge.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.xxx.core.feature.orm.mybatis.Page;
import cn.xxx.rdc.knowledge.dao.KbSearchPageMapper;
import cn.xxx.rdc.knowledge.dto.KbSearchInParamDto;
import cn.xxx.rdc.knowledge.dto.KbSearchOutParamDto;
import cn.xxx.rdc.knowledge.service.KbSearchPageService;
import lombok.extern.slf4j.Slf4j;

/**
 * 搜索服务实现类
 * 
 * @author xxx
 * @date: 2021-08-07 08:57:45
 * @Copyright: Copyright (c) 2006 - 2021
 * @Company: xxx
 * @Version: V1.0
 */
@Slf4j
@Service
public class KbSearchPageServiceImpl implements KbSearchPageService {

    @Autowired
    KbSearchPageMapper kbSearchPageMapper;

    /** 注入transportClient */
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private final String ES_INDEX = "knowledge";

    @Override
    public List listByParams(Page page, KbSearchInParamDto params) {
        log.info("KbSearchPageServiceImpl listByParams start");

        // 1. 创建批量搜索请求,并绑定索引
        SearchRequest searchRequest = new SearchRequest(this.ES_INDEX);
        // 2. 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        Long menu_id = params.getMenuId();
        if (null != menu_id && !menu_id.equals(new Long(0))) {
            boolQueryBuilder.must(QueryBuilders.termQuery("menuId", menu_id));
        }
        String keyword = params.getKeyword();
        boolQueryBuilder.should(QueryBuilders.matchQuery("title", keyword));
        boolQueryBuilder.should(QueryBuilders.matchQuery("content", keyword));
        // 最小匹配
        boolQueryBuilder.minimumShouldMatch(1);
        sourceBuilder.query(boolQueryBuilder);
        // 设置分页参数
        int from = (page.getPageNo() - 1) * page.getPageSize();
        int size = page.getPageSize();
        sourceBuilder.from(from);
        sourceBuilder.size(size);
        // 3. 将查询条件放入搜索请求request中
        searchRequest.source(sourceBuilder);

        // 4. 发起查询请求获取数据
        SearchResponse response = null;
        try {
            response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            String msg = "查询ES异常!";
            log.error(msg, e);
            return null;
        }
        List result = new ArrayList();
        SearchHit[] hits = response.getHits().getHits();
        if (null != hits && hits.length > 0) {
            for (SearchHit searchHit : hits) {
                KbSearchOutParamDto d = new KbSearchOutParamDto();
                d = JSONObject.parseObject(JSONObject.toJSONString(searchHit.getSourceAsMap()),
                    KbSearchOutParamDto.class);
                result.add(d);
            }
        }
        log.info("KbSearchPageServiceImpl listByParams end");
        return result;
    }

}

以上代码,跟xxx相关的都为业务代码,无需关心,只需要保留非xxx的代码即可

你可能感兴趣的