ElasticSearch实现搜索功能

      引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

在测试类或者后台将数据库数据导入到es中

 es中只放需要在页面显示的数据就行了,其他的交给详情页面

 @Test
    public void initEs() throws IOException {
        List<Product> products = productMapper.selectProduct();
        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);

            String productId = product.getProductId();
            String productName = product.getProductName();
            Integer soldNum = product.getSoldNum();
            List<ProductSku> productSkuList = product.getProductSkuList();
            String skuName = "";
            String skuImg = "";
            Integer sellPrice = 0;
            if (productSkuList.size() > 0){
               skuName = productSkuList.get(0).getSkuName();
               skuImg = productSkuList.get(0).getSkuImg();
               sellPrice = productSkuList.get(0).getSellPrice();
            }
            ProductEs productEs = new ProductEs(productId, productName, skuImg, soldNum,skuName, sellPrice);

            IndexRequest request = new IndexRequest("fmmallproducts");
            request.id(productId);
            request.source(objectMapper.writeValueAsString(productEs),XContentType.JSON);

            IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            System.out.println("indexResponse = " + indexResponse);


        }
    }

从es库中取数据

 public ResultVo getProductsByKeyword(String keyword, int pageNum, int limit) {
        ResultVo resultVo = null;
        try {
            // 开始查询的记录数
            int start = (pageNum - 1) * limit;
            // 从ES查询数据
            SearchRequest request = new SearchRequest("fmmallproducts");
            // 查询条件
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery(keyword, "productName", "productSkuName"));

            //分页条件
            searchSourceBuilder.from(start);
            searchSourceBuilder.size(limit);
            // 高亮显示
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field productName = new HighlightBuilder.Field("productName");
            HighlightBuilder.Field productSkuName = new HighlightBuilder.Field("productSkuName");
            highlightBuilder.field(productName);
            highlightBuilder.field(productSkuName);
            highlightBuilder.preTags("<label style='color:red'>");
            highlightBuilder.postTags("</label>");
            // 高亮封装
            searchSourceBuilder.highlighter(highlightBuilder);
            // 查询封装
            request.source(searchSourceBuilder);

            //执行搜索
            SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            // 获取命中的数据
            SearchHits hits = searchResponse.getHits();
            // 获取总记录数
            int count = (int) hits.getTotalHits().value;


            // mysql模糊查询
            //String kw = "%" + keyword + "%";
            //List<Product> products = productMapper.selectProductByKeyword(keyword, start, limit);
            // 查询总记录数
            //Example example = new Example(Product.class);
            //Example.Criteria criteria = example.createCriteria();
            //criteria.andLike("productName", kw);
            //int count = productMapper.selectCountByExample(example);



            //  计算总页数
            int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
            Iterator<SearchHit> iterator = hits.iterator();
            // 封装查询结果
            ArrayList<ProductEs> products = new ArrayList<>();
            while (iterator.hasNext()){
                SearchHit searchHit = iterator.next();
                ProductEs productEs = objectMapper.readValue(searchHit.getSourceAsString(), ProductEs.class);
                // 获取高亮字段
                Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
                HighlightField field1 = highlightFields.get("productName");
                if (field1 != null){
                   String highLightProductName = Arrays.toString(field1.fragments());
                    productEs.setProductName(highLightProductName);
                }
                products.add(productEs);
            }


            // 封装返回数据
            PageHelper<Product> pageHelper = new PageHelper(count, pageCount, products);

            resultVo = ResultVo.ok().data("pageHelper", pageHelper);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultVo;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值