前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot集成elasticsearch

springboot集成elasticsearch

作者头像
小勇DW3
发布2019-05-10 10:34:42
7500
发布2019-05-10 10:34:42
举报
文章被收录于专栏:小勇DW3小勇DW3

  在基础阶段学习ES一般是首先是 安装ES后借助 Kibana 来进行CURD 了解ES的使用;

  在进阶阶段可以需要学习ES的底层原理,如何通过Version来实现乐观锁保证ES不出问题等核心原理;

  第三个阶段就是学以致用 ,在项目中如何做到 springboot集成elasticsearch来解决实际问题,下边通过一个Demo的介绍过程来引导学习。

   1、首先pom.xml配置所需jar包,jar使用的版本需要和测试环境上的es保持配套;

代码语言:javascript
复制
<!-- elasticsearch 5.x 依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <!-- <scope>test</scope> -->
        </dependency>

   2、配置类:

代码语言:javascript
复制
@Configuration
public class ESConfiguration implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {
    
    private static final Logger logger = LoggerFactory.getLogger(ESConfiguration.class);
    
    /**
     * es集群地址
     */
    @Value("${elasticsearch.ip}")
    private String hostName;
    /**
     * 端口
     */
    @Value("${elasticsearch.port}")
    private String port;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.cluster.name}")
    private String clusterName;
    
    /**
     * 连接池
     */
    @Value("${elasticsearch.pool}")
    private String poolSize;
    
    private TransportClient client;

    @Override
    public void destroy() throws Exception {
        try {
            logger.info("Closing elasticSearch client");
            if (client != null) {
                client.close();
            }
        } catch (final Exception e) {
            logger.error("Error closing ElasticSearch client: ", e);
        }
    }

    @Override
    public TransportClient getObject() throws Exception {
        return client;
    }

    @Override
    public Class<TransportClient> getObjectType() {
        return TransportClient.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            // 配置信息
            Settings esSetting = Settings.builder().put("cluster.name", clusterName).put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群
                    .put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5
                    .build();

            client = new PreBuiltTransportClient(esSetting);
            InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
            client.addTransportAddresses(inetSocketTransportAddress);

        } catch (Exception e) {
            logger.error("elasticsearch TransportClient create error!!!", e);
        }
    }

}

 3、涉及controller层

代码语言:javascript
复制
@RestController
@RequestMapping("/search")
public class SearchRestController extends BaseController{
    
    private static final Logger log = LoggerFactory.getLogger(SearchRestController.class);

    @Autowired
    private ESSearchService esSearchService;
    
    @Autowired
    private ESAggsSearchService eSAggsSearchService;
    
    @Autowired
    private ESSuggestSearchService esSuggestSearchService;

    /**
     * 关键字查询
     * @param index
     * @return
     */
    @RequestMapping(value = "/test")
    public ResponseVo<?> test(
            @RequestParam(value = "index", required = false) String index,
            @RequestParam(value = "filed", required = false) String filed,
            @RequestParam(value = "keyWord", required = false) String keyWord
    ) throws Exception{
        //判空
        List<String> searchList = esSearchService.searchMessageByKeyWord(index, filed, keyWord, 10, 0);
        return generateResponseVo(ESWebStatusEnum.SUCCESS, searchList);
    }
    
    /**
     * 构建索引
     * @param index
     * @return
     */
    @RequestMapping(value = "/buildIndex")
    public ResponseVo<?> buildIndex(@RequestParam(value = "index", required = false) String index)
    {
        //判空
        if(index == null) {
            return generateResponseVo(ESWebStatusEnum.FAILED, null);
        }
        esSearchService.buildIndex(index);
        return generateResponseVo(ESWebStatusEnum.SUCCESS, null);
    }
    
    /*@RequestMapping(value = "/delIndex")
    public ResponseVo<?> delIndex(
            ) {
        
        for(int j=1; j<7; j++) {
            for(int i=1; i<=30; i++) {
                StringBuilder sb = new StringBuilder("forum2018-0");
                sb.append(j);
                sb.append("-");
                
                if(i < 10) {
                    sb.append("0" + i);
                }else {
                    sb.append(i);
                }
                try { 
                    esSearchService.delIndex(sb.toString());
                }catch(Exception e) {
                    System.out.println("继续");
                }
            }
        }
        
        return generateResponseVo(ESWebStatusEnum.SUCCESS, null);
    }*/
    
    /**
     *  查询数据
     *  
     * @param index
     * @param type
     * @param id
     * @return
     */
    //http://localhost:8088/search/data?index=suggest_term_index$type=tech&id=AWpNa9UkTc7xOmAB67Cu
    @RequestMapping(value = "/data")
    @ResponseBody
    public ResponseVo<?> search(
            @RequestParam(value = "index", required = false) String index,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "id", required = false) String id
            ) {
        //判空
        if(index == null || type == null || id == null) {
            return generateResponseVo(ESWebStatusEnum.FAILED, null);
        }
        //搜索具体的数据来源
        Map<String, Object> returnMap = esSearchService.searchDataByParam("suggest_term_index", "tech", "AWpNa9UkTc7xOmAB67Cu");
        return generateResponseVo(ESWebStatusEnum.SUCCESS, returnMap);
    }
    
    /**
     * 增加索引
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/build_suggest_index")
    @ResponseBody
    public ResponseVo<?> build_suggest_index(
            ) throws Exception {
        //搜索具体的数据来源
        
//        String index = "search_suggest_index";
        String term_index = "suggest_term_index";
        esSuggestSearchService.buildIndexByParam(term_index);
        
        return generateResponseVo(ESWebStatusEnum.SUCCESS, null);
        
    }
    
    /**
     * 加入数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addDataToIndex")
    @ResponseBody
    public ResponseVo<?> addDataToIndexForSuggest() throws Exception
    {
        String term_index = "suggest_term_index";
        
        //搜索具体的数据来源
        SuggestModel data = new SuggestModel();
        data.setContent("联合信用股份有限公司");//北京联合信用投资咨询有限公司,联合信用投资咨询有限公司
        data.setId(1l);
        data.setData(12);
        
        esSuggestSearchService.addDataDocForSuggest( term_index, "tech", data);
        
        SuggestModel data1 = new SuggestModel();
        data1.setContent("北京联合信用投资咨询有限公司");//,联合信用投资咨询有限公司
        data1.setId(1l);
        data1.setData(12);
        
        esSuggestSearchService.addDataDocForSuggest( term_index, "tech", data1);
        
        SuggestModel data2 = new SuggestModel();
        data2.setContent("联合信用投资咨询有限公司");//,
        data2.setId(1l);
        data2.setData(12);
        esSuggestSearchService.addDataDocForSuggest( term_index, "tech", data2);
        
        return generateResponseVo(ESWebStatusEnum.SUCCESS, null);
    }
    
    /**
     * JSON格式化插入数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addJSONDataDoc")
    @ResponseBody
    public ResponseVo<?> addJSONDataDoc() throws Exception
    {
        String index = "bbs_post_index";
        
        ModelMap map = new ModelMap();
        map.put("collectCount", 0);
        map.put("commentCount", 0);
        map.put("content", "压力测试,<a data-name=\"themeName\" href=\"#searchList?search=债券市场&type=2\" data-value=\"债券市场\" contenteditable=\"false\" class=\"comment-a\">#债券市场#</a> ,<a data-name=\"bondName\" href=\"#bondInfo/b6028d34b4b16ed2bf3513dcca91daa0\" data-value=\"b6028d34b4b16ed2bf3513dcca91daa0\" contenteditable=\"false\" class=\"comment-a\">$12进出12(120312.IB)$</a> 是发的这只债券吗? okokok,<a data-name=\"entityName\" href=\"#entityInfo/2029149\" data-value=\"2029149\" contenteditable=\"false\" class=\"comment-a\">$浙江省手工业合作社联合社$</a> 是不是和公司");
        map.put("createTime", "2018-09-03 13:49:51");
        map.put("downloadCount", 0);
        map.put("forwardCount", 0);
        map.put("id", 773);
        map.put("isAnonymity", 0);
        map.put("originalContent", "压力测试,#债券市场# ,$12进出12(120312.IB)$ 是发的这只债券吗? okokok,$浙江省手工业合作社联合社$ 是不是和公司");
        map.put("postVariety", 0);
        map.put("readCount", 0);
        map.put("type", 1);
        map.put("updateTime", "2018-09-03 13:49:51");
        map.put("userId", 241);
        map.put("valid", 1);
        
        String esId = esSearchService.addJSONDataDoc(index, "post", map);
        
        return generateResponseVo(ESWebStatusEnum.SUCCESS, esId);
    }
    
    /**
     * 封装参数进行查询
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSearchByParam")
    @ResponseBody
    public ResponseVo<?> getSearchByParam(
            ) throws Exception {
        //搜索具体的数据来源
        
        BasicSearchParam param = new BasicSearchParam();
        param.setIndex("bbs_post_index");
        param.setField("content");
        param.setDistictField("id");
        param.setKeyWord("压力测试");
        param.setLimit(10);
        param.setOffset(0);
        
        /*List<String> list = esSearchService.searchMsgByParam(param);
        Long count = esSearchService.searchMsgCountByParam(param);
        System.out.println(JSONObject.toJSONString(list));
        System.out.println(count);*/
        
        BootstrapTablePaginationVo<String> vo = eSAggsSearchService.searchMsgByParam(param);
        
        return generateResponseVo(ESWebStatusEnum.SUCCESS, vo);
    }
    
}

 4、service层

代码语言:javascript
复制
@Service
public class ESAggsSearchImpl implements ESAggsSearchService {

    @Autowired
    private ESRepository eSRepository;
    
    @Autowired
    private ESAggsRepository eSAggsRepository;

    @Override
    public BootstrapTablePaginationVo<String> searchMsgByParam(BasicSearchParam param) throws Exception {
        return eSAggsRepository.searchMsgByParam(param);
    }

}
代码语言:javascript
复制
@Service
public class ESDeleteServiceImpl implements EsDeleteService{

    @Autowired
    private ESDeleteRepository esDeleteRepository;

    @Override
    public boolean delDataById(DeleteParam esDeleteParam) {
        return esDeleteRepository.delDataById(esDeleteParam);
    }

    
}
代码语言:javascript
复制
@Service
public class ESSearchServiceImpl implements ESSearchService{

    @Autowired
    private ESRepository eSRepository;
    
    @Autowired
    private ESSuggestRepository eSSuggestRepository;

    @Override
    public boolean buildIndex(String index) {
        return eSRepository.buildIndex(index);
    }

    @Override
    public int addPostDataDoc(String postId, String postContent) throws Exception {
        return eSRepository.addPostDataDoc(postId, postContent);
    }

    @Override
    public String addJSONDataDoc(String index, String type, Object obj) throws Exception {
        return eSRepository.addJSONDataDoc(index, type, obj);
    }

    @Override
    public void matchQuery(String keyWord, String index, int limit, int offset) throws Exception {
        eSRepository.matchQuery(keyWord, index, limit, offset);
    }

    @Override
    public Map<String, Object> searchDataByParam(String index, String type, String id) {
        return eSRepository.searchDataByParam(index, type, id);
    }

    @Override
    public String addTargetDataALL(JSONObject data, String index, String type, String id) {
        return eSRepository.addTargetDataALL(data, index, type, id);
    }

    @Override
    public boolean isIndexExist(String index) {
        return eSRepository.isIndexExist(index);
    }

    @Override
    public Iterator<MultiGetItemResponse> multiGetData(List<Item> itemList) {
        return eSRepository.multiGetData(itemList);
    }

    @Override
    public List<String> searchMessageByKeyWord(String index, String filed, String keyWord, int limit, int offset) throws Exception {
        return eSRepository.searchMessageByKeyWord(index, keyWord, limit, offset);
    }

    @Override
    public List<String> searchMsgByParam(BasicSearchParam param) throws Exception {
        return eSRepository.searchMsgByParam(param);
    }

    @Override
    public Long searchMsgCountByParam(BasicSearchParam param) throws Exception {
        return eSRepository.searchMsgCountByParam(param);
    }

    
}

 5、dao层,应该是最重要的层,主要用来进行操作es;

代码语言:javascript
复制
@Component
public class ESRepository extends BaseRepository{

    private static final Logger LOG = LoggerFactory.getLogger(ESRepository.class);

    @Autowired
    private TransportClient client;
    
    /**
     * 增加文档,测试用的- 增加文档
     * 
     * @param post
     * @return
     * @throws Exception
     */
    public int addPostDataDoc(String postId, String postContent) throws Exception {
        IndexResponse response = client.prepareIndex("forum_index", "post").setSource(XContentFactory.jsonBuilder().startObject().field("id", postId).field("content", postContent).endObject()).get();
        return response.hashCode();
    }
    
    /**
     * 搜索
     * @param param
     * @return
     * @throws Exception
     */
    public List<String> searchMsgByParam(BasicSearchParam param) throws Exception {
        String keyWord = param.getKeyWord();
        String filed = param.getField();
        String index = param.getIndex();
        
        Assert.assertNotNull(client);
        Assert.assertNotNull(filed);
        Assert.assertNotNull(index);
        Assert.assertNotNull(keyWord);
        
        //校验索引是否成功
        if (!isIndexExist(index)) {
            return null;
        }
        
        //响应信息
        List<String> responseStrList = new ArrayList<String>();
        //去重的信息
        CollapseBuilder cb = new CollapseBuilder(param.getDistictField());
        
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(filed, keyWord);
        //查询
        SearchResponse response = client.prepareSearch(index)
                                        .setQuery(matchQueryBuilder)
                                        .setCollapse(cb)
                                        .setFrom(param.getOffset())
                                        .setSize(param.getLimit())
                                        .get();
        
        SearchHits shList = response.getHits();
        for (SearchHit searchHit : shList) {  
            responseStrList.add(searchHit.getSourceAsString());
        }  
        return responseStrList;
    }
    
    /**
     * 搜索
     * @param param
     * @return
     * @throws Exception
     */
    public Long searchMsgCountByParam(BasicSearchParam param) throws Exception {
        String keyWord = param.getKeyWord();
        String filed = param.getField();
        String index = param.getIndex();
        
        Assert.assertNotNull(client);
        Assert.assertNotNull(filed);
        Assert.assertNotNull(index);
        Assert.assertNotNull(keyWord);
        
        //校验索引是否成功
        if (!isIndexExist(index)) {
            return null;
        }
        
        //去重的信息
        CollapseBuilder cb = new CollapseBuilder(param.getDistictField());
        
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(filed, keyWord);
        SearchResponse response = client.prepareSearch(index)
                                .setQuery(matchQueryBuilder)
                                .setCollapse(cb)
                                .get();
        
        SearchHits shList = response.getHits();
        return shList.totalHits;
    }

    /**
     * 查询
     * 
     * @param keyWord
     * @param index
     * @param limit
     * @param offset
     * @return
     * @throws Exception
     */
    public void matchQuery(String keyWord, String index, int limit, int offset) throws Exception {
        TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery("content", keyWord);
        SearchResponse response = client.prepareSearch(index).setQuery(queryBuilder).setFrom(offset).setSize(limit).get();
        for (SearchHit searchHit : response.getHits()) {
            String sourceStr = searchHit.getSourceAsString();
            LOG.info("matchQuery-->>>" + sourceStr);
        }
    }

    /**
     * 批量查询
     * 
     * 备注: 1、批量查询是再你知道下面的属性的时候,才去批量查询,如果都不知道Index,type就 直接查询,那个是ES搜索,不是批量查询
     * 2、批量查询能提高程序查询效率,根据需求自我添加
     * 
     * Item 类结构里有属性,index<==>_index,type<==>_type,id<==>_id
     * 
     * 下面是es文档结构 { "_index": "bond2018-03-15", "_type": "bond", "_id":
     * "AWIoxzdzUfSIA3djz-ZK", "_score": 1, "_source": { "code": "130523",
     * "@timestamp": "2018-03-15T16:29:27.214Z", "name": "15福建09", "@version":
     * "1", "id": 13293, "type": "bond", "tags": [ ], "timestamp":
     * "2018-03-15T16:29:27.214Z" } }
     * 
     * @param itemList
     * @return
     */
    public Iterator<MultiGetItemResponse> multiGetData(List<Item> itemList) {
        if (!CollectionUtils.isEmpty(itemList)) {
            MultiGetRequestBuilder mgrb = client.prepareMultiGet();
            itemList.forEach(item -> {
                mgrb.add(item);
            });
            MultiGetResponse response = mgrb.get();
            // 查询
            Iterator<MultiGetItemResponse> itMultigetItem = response.iterator();
            return itMultigetItem;
        }
        return null;
    }

    /**
     * 用户添加索引数据文档
     * 
     * @param index
     *            对应的数据库
     * @param type
     *            类型 对应mysql的数据表
     * @param obj
     *            可以添加目标类
     * @return
     * @throws Exception
     */
    public int addTargetObjectDataDoc(String index, String type, Object obj) throws Exception {
        // 构建参数和需要属性
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        Assert.assertNotNull(type);
        XContentBuilder xb = XContentFactory.jsonBuilder().startObject();

        // 下面是反射处理传来的Object类,对应每个字段映射到对应的索引里,如果不需要这么做的,就可以注释掉下面的代码
        // 得到类对象
        Class<?> userCla = (Class<?>) obj.getClass();
        // 得到类中的所有属性集合
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {// 遍历obj文档的字段字段,添加到数据里
            Field f = fs[i];
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = new Object();
            val = f.get(obj);
            // 得到此属性的值
            xb.field(f.getName(), val);
        }

        // 返回数据来源

        IndexResponse indexResponse = client.prepareIndex().setIndex(index).setType(type)
        // .setId(id) // 如果没有设置id,则ES会自动生成一个id
                .setSource(xb.endObject()).get();
        LOG.info("添加document,index:" + index + ",type:" + type + ",目标类obj:" + JSONObject.toJSONString(obj));
        return indexResponse.hashCode();
    }

    /**
     * 查询数据
     * 
     * @param index
     *            索引<----->关系型数据库
     * @param type
     *            类型<----->关系型数据表
     * @param id
     *            数据ID<----->id
     * @return
     */
    public Map<String, Object> searchDataByParam(String index, String type, String id) {
        if (index == null || type == null || id == null) {
            LOG.info(" 无法查询数据,缺唯一值!!!!!!! ");
            return null;
        }
        // 来获取查询数据信息 - 查询依据: index type id
        GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
        GetResponse getResponse = getRequestBuilder.execute().actionGet();
        // 这里也有指定的时间获取返回值的信息,如有特殊需求可以

        return getResponse.getSource();
    }

    /**
     * 更新数据
     *
     * @param data
     *            添加的数据类型 json格式的
     * @param index
     *            索引<----->关系型数据库
     * @param type
     *            类型<----->关系型数据表
     * @param id
     *            数据ID<----->id
     * @return
     */
    public void updateDataById(JSONObject data, String index, String type, String id) {
        if (index == null || type == null || id == null) {
            LOG.info(" 无法更新数据,缺唯一值!!!!!!! ");
            return;
        }

        // 更新步骤
        UpdateRequest up = new UpdateRequest();
        up.index(index).type(type).id(id).doc(data);

        // 获取响应信息
        // .actionGet(timeoutMillis),也可以用这个方法,当过了一定的时间还没得到返回值的时候,就自动返回。
        UpdateResponse response = client.update(up).actionGet();
        LOG.info("更新数据状态信息,status{}", response.status().getStatus());
    }

    /**
     * 添加数据
     *
     * @param data
     *            添加的数据类型 json格式的
     * @param index
     *            索引<----->关系型数据库
     * @param type
     *            类型<----->关系型数据表
     * @param id
     *            数据ID<----->id
     * @return
     */
    public String addTargetDataALL(JSONObject data, String index, String type, String id) {
        // 判断一下次id是否为空,为空的话就设置一个id
        if (id == null) {
            id = UUID.randomUUID().toString();
        }
        // 正式添加数据进去
        IndexResponse response = client.prepareIndex(index, type, id).setSource(data).get();

        LOG.info("addTargetDataALL 添加数据的状态:{}", response.status().getStatus());

        return response.getId();
    }
    
    /**
     * JSON字符串加入到es里
     * @param index
     * @param type
     * @param obj
     * @return
     * @throws Exception
     */
    public String addJSONDataDoc(String index, String type, Object obj) throws Exception{  
        //构建参数和需要属性
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        Assert.assertNotNull(type);

        client.prepareIndex().setIndex(index).setType(type).setSource();
        
        //返回数据来源
        IndexResponse indexResponse = client.prepareIndex().setIndex(index).setType(type).setSource(JSONObject.toJSONString(obj), XContentType.JSON).get();
        LOG.debug("添加document,index:" + index + ",type:" + type + ",目标类obj:" + JSONObject.toJSONString(obj));
        return indexResponse.getId();
    } 

    /**
     * 判断索引是否存在
     *
     * @param index
     * @return
     */
    public boolean isIndexExist(String index) {
        IndicesExistsResponse iep = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
        if (iep.isExists()) {
            LOG.info("此索引 [" + index + "] 已经在ES集群里存在");
        } else {
            LOG.info(" 没有此索引 [" + index + "] ");
        }
        return iep.isExists();
    }

    /**
     * 根据关键词查询
     * 
     * @param keyWord
     *            搜索词
     * @param index
     *            索引
     * @param limit
     *            分页参数
     * @param offset
     *            分页参数
     * @return
     * @throws Exception
     */
    public List<String> searchMessageByKeyWord(String index, String keyWord, int limit, int offset) throws Exception {
        List<String> responseStrList = new ArrayList<String>();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("content", keyWord);
        SearchResponse response = client.prepareSearch(index).setQuery(matchQueryBuilder).setFrom(offset).setSize(limit).get();
        for (SearchHit searchHit : response.getHits()) {
            responseStrList.add(searchHit.getSourceAsString());
        }
        return responseStrList;
    }
    
     /**
     * @param index
     * @param filed
     * @param keyWord
     * @param limit
     * @param offset
     * @return
     */
    public List<String> search_IdByKeyWord(String index, String filed, String keyWord, int limit, int offset) {  
        LOG.debug("es serarch index->" + index + ",filed->" + filed + ",keyWord->" + keyWord);
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        Assert.assertNotNull(keyWord);
        List<String> responseStrList = new ArrayList<String>();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(filed, keyWord);
        
        SearchResponse response = client.prepareSearch(index).setQuery(matchQueryBuilder).setFrom(offset).setSize(limit).get();
        
        for (SearchHit searchHit : response.getHits()) {  
            responseStrList.add(searchHit.getId());
        }  
        return responseStrList;
    }
    
    /**
     * 根据关键词查询,使用的查询是term_query
     * @param index
     * @param filed
     * @param keyWord
     * @param limit
     * @param offset
     * @return
     */
    public List<String> searchMessageTermQueryByKeyWord(String index, String filed, String keyWord, int limit,
            int offset) {  
        LOG.info("es serarch index->" + index + ",filed->" + filed + ",keyWord->" + keyWord);
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        Assert.assertNotNull(keyWord);
        List<String> responseStrList = new ArrayList<String>();
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(filed, keyWord);
        
        //查询信息
        SearchResponse response = client.prepareSearch(index).setQuery(termsQueryBuilder).setFrom(offset).setSize(limit).get();
        for (SearchHit searchHit : response.getHits()) {  
            responseStrList.add(searchHit.getSourceAsString());
        }  
        return responseStrList;
    }
    
    /**
     * 根据关键词查询,使用的查询是match_phrase
     * @param index
     * @param filed
     * @param keyWord
     * @param limit
     * @param offset
     * @return
     */
    public List<String> searchMessageMatchPhraseQueryByKeyWord(String index, String filed, String keyWord, int limit,
            int offset) {  
        LOG.info("es serarch index->" + index + ",filed->" + filed + ",keyWord->" + keyWord);
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        Assert.assertNotNull(keyWord);
        List<String> responseStrList = new ArrayList<String>();
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery(filed, keyWord);
        
        SearchResponse response = client.prepareSearch(index).setQuery(matchPhraseQueryBuilder).setFrom(offset).setSize(limit).get();
        for (SearchHit searchHit : response.getHits()) {  
            responseStrList.add(searchHit.getSourceAsString());
        }  
        return responseStrList;
    }
    
    /**
     * 根据关键词查询  分页查询
     * @param filedMap 搜索关键词Map key 是要搜索的字段 value是关键词
     * @param index 索引,库
     * @param limit
     * @param offset
     * @param filed 字段
     * @return
     * @throws Exception
     */
    public List<String> searchMessageByMapKeyWord(String index, Map<String, String> filedMap, int limit, int offset) throws Exception{  
        LOG.info("es serarch index->" + index + ",filedMap->" + JSONObject.toJSONString(filedMap));
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        List<String> responseStrList = new ArrayList<String>();
        
        QueryBuilder finalQueryBuilder = null;
        if(!CollectionUtils.isEmpty(filedMap)) {
            for(Map.Entry<String, String> entry : filedMap.entrySet()) {
                String key = entry.getKey(); //key 是要搜索的字段
                String value = entry.getValue();//value是关键词
                
                TermQueryBuilder termQueryBuilder1 = QueryBuilders.termQuery(key, value);
                finalQueryBuilder = QueryBuilders.boolQuery().must(termQueryBuilder1);
            }
        }
        //query
        SearchResponse response = client.prepareSearch(index).setQuery(finalQueryBuilder).setFrom(offset).setSize(limit).get();  
        for (SearchHit searchHit : response.getHits()) {  
            responseStrList.add(searchHit.getSourceAsString());
        }  
        return responseStrList;
    } 
    
    /**
     * 根据关键词查询  获取总数
     * @param filedMap 搜索关键词Map key 是要搜索的字段 value是关键词
     * @param index 索引,库
     * @param limit
     * @param offset
     * @param filed 字段
     * @return
     * @throws Exception
     */
    public long searchMessageByMapKeyWordCount(String index, Map<String, String> filedMap) throws Exception{  
        LOG.info("es serarch index->" + index + ",filedMap->" + filedMap);
        Assert.assertNotNull(client);
        Assert.assertNotNull(index);
        QueryBuilder finalQueryBuilder = null;
        if(!CollectionUtils.isEmpty(filedMap)) {
            for(Map.Entry<String, String> entry : filedMap.entrySet()) {
                String key = entry.getKey(); //key 是要搜索的字段
                String value = entry.getValue();//value是关键词
                
                TermQueryBuilder termQueryBuilder1 = QueryBuilders.termQuery(key, value);
                finalQueryBuilder = QueryBuilders.boolQuery().must(termQueryBuilder1);
            }
        }
        long count = client.prepareSearch(index).setQuery(finalQueryBuilder).get().getHits().totalHits;
        return count;
    } 

    public List<String> searchMessageByKeyWord(String index, String filed, String keyWord, int limit, int offset) throws Exception {
        List<String> responseStrList = new ArrayList<String>();
        TermQueryBuilder matchQueryBuilder = QueryBuilders.termQuery(filed, keyWord);
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("userId", "251");
        QueryBuilder finalQueryBuilder = QueryBuilders.boolQuery().must(matchQueryBuilder).must(termQueryBuilder);

        SearchResponse response = client.prepareSearch(index).setQuery(finalQueryBuilder).setFrom(offset).setSize(limit).get();
        
        for (SearchHit searchHit : response.getHits()) {
            responseStrList.add(searchHit.getSourceAsString());
        }
        return responseStrList;
    }
    
}

 7、springboot配置文件

代码语言:javascript
复制
# Elasticsearch
elasticsearch.cluster.name=elasticsearch
elasticsearch.ip=127.0.0.1
elasticsearch.port=9300
elasticsearch.pool=5

server.port=8088

 8、测试结果

 使用浏览器进行访问结果测试:

使用kibana进行测试,结果是一样的;

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档