ElastSearch整合SpringBoot模仿京东商城实现关键字高亮显示

Posted 梦与光同行

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElastSearch整合SpringBoot模仿京东商城实现关键字高亮显示相关的知识,希望对你有一定的参考价值。

前提:下载Elasticsearch服务并开启,使用SpringBoot集成Elasticsearch客户端

用到的技术:Springboot+Vue+Thymeleaf+Elasticsearch

效果如下


接下来上代码(不分前后顺序)

页面跳转controller

package com.wyh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @program: SpringBoot_ElasticSearch
 * @description: index页面跳转controller
 * @author: 魏一鹤
 * @createDate: 2022-04-19 23:09
 **/
@Controller
public class IndexController 
    @GetMapping("/","/index")
    public String index() 
        return "index";
    


接口数据请求controller

package com.wyh.controller;

import com.wyh.service.JDContentService;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: SpringBoot_ElasticSearch_JD
 * @description: 京东商品控制器
 * @author: 魏一鹤
 * @createDate: 2022-04-20 00:48
 **/

//前端请求编写
@RestController
public class JDContentController 

    @Autowired
    private JDContentService jdContentService;
    //测试es批量添加京东数据 restful风格
    @GetMapping("/parse/keyword")
    public boolean parse(@PathVariable("keyword") String keyword) throws IOException 
      return  jdContentService.parseContent(keyword);
    

    //测试查询
    @GetMapping("/search/keyword/pageNo/pageSize")
    public  List<Map<String,Object>> search(@PathVariable("keyword") String keyword,
                                            @PathVariable("pageNo") int pageNo,
                                            @PathVariable("pageSize")int pageSize) throws IOException 
       return  jdContentService.searchPageHighLight(keyword,pageNo,pageSize);

    



京东商品实体

package com.wyh.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @program: SpringBoot_ElasticSearch_JD
 * @description: JD内容实体
 * @author: 魏一鹤
 * @createDate: 2022-04-20 00:37
 **/

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class JDContent 
    private String title;
    private String img;
    private String  price;
    //可以自定义继续加属性

京东商品service

package com.wyh.service;

import com.alibaba.fastjson.JSON;
import com.wyh.entity.JDContent;
import com.wyh.utils.htmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
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.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: SpringBoot_ElasticSearch_JD
 * @description: 京东商品服务接口
 * @author: 魏一鹤
 * @createDate: 2022-04-20 00:47
 **/

//业务编写
@Service
public class JDContentService 
    //注入bean
    @Resource
    private RestHighLevelClient client;
    //1 解析数据 放入es索引库
    public boolean parseContent(String keywords) throws IOException 
        ArrayList<JDContent> jdContents = new HtmlParseUtil().parseJD(keywords);
        //把查询并且解析好的数据批量插入到es库
        BulkRequest bulkRequest = new BulkRequest();
        //过期时间2分钟
        bulkRequest.timeout("2m");
        //批量插入数据
        for (int i = 0; i < jdContents.size(); i++) 
            bulkRequest.add(new IndexRequest("jd_goods")
                    //这里就不给id赋值了 随时生成
                    .source(JSON.toJSONString(jdContents.get(i)),XContentType.JSON)
            );
        
        //执行批量插入请求
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //没有返回失败就是成功
        return !bulk.isFragment();

    
    //2 获取这些数据  实现搜索功能
    public List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException 
        //当前页数第一条
        if(pageNo<=1)
            pageNo=1;
        
        //条件搜索
        SearchRequest searchRequest = new SearchRequest("jd_goods");
        //构建查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);
        //分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);
        //精准匹配关键字
        SearchSourceBuilder query = searchSourceBuilder.query(termQueryBuilder);
        //超时时间
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        //创建map 用于返回
        ArrayList<Map<String,Object>> arrayList = new ArrayList();
        //解析结果
        for (SearchHit document : searchResponse.getHits().getHits()) 
            arrayList.add(document.getSourceAsMap());
        
        return arrayList;
    

    //3 实现搜索高亮的功能
    public List<Map<String,Object>> searchPageHighLight(String keyword,int pageNo,int pageSize) throws IOException 
        //当前页数第一条
        if(pageNo<=1)
            pageNo=1;
        
        //条件搜索
        SearchRequest searchRequest = new SearchRequest("jd_goods");
        //构建查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);
        //分页
        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);
        //精准匹配关键字
        SearchSourceBuilder query = searchSourceBuilder.query(termQueryBuilder);
        //超时时间
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        //高亮显示
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //让标题高亮
        highlightBuilder.field("title");
        //高亮的前缀
        highlightBuilder.preTags("<span style='color:red'>");
        //高亮的后缀
        highlightBuilder.postTags("</span>");
        //关闭多个高亮
        highlightBuilder.requireFieldMatch(false);
        searchSourceBuilder.highlighter(highlightBuilder);

        //执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        //创建map 用于返回
        ArrayList<Map<String,Object>> arrayList = new ArrayList();
        //解析结果
        for (SearchHit document : searchResponse.getHits().getHits()) 
            //获取高亮的字段
            Map<String, HighlightField> highlightFields = document.getHighlightFields();
            HighlightField title = highlightFields.get("title");
            //原来的结果
            Map<String, Object> sourceAsMap = document.getSourceAsMap();
            //解析高亮的字段,将原来的字段换成高亮的字段即可
            //如果高亮字段存在
            if(title!=null)
                Text[] fragments = title.fragments();
                //新标题
                String newTitle="";
                for (Text fragment : fragments) 
                    newTitle+=fragment;
                
                //高亮字段替换原来的字段内容即可
                sourceAsMap.put("title",newTitle);
            
            arrayList.add(sourceAsMap);
        
        return arrayList;
    

解析页面工具包

package com.wyh.utils;

import com.wyh.entity.JDContent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: SpringBoot_ElasticSearch_JD
 * @description: jsoup解析网页
 * @author: 魏一鹤
 * @createDate: 2022-04-19 23:53
 **/

@Component
public class HtmlParseUtil 
    //public static void main(String[] args) throws IOException 
    //    //ArrayList<JDContent> jdContents = parseJD("你好");
    //    //for (JDContent jdContent : jdContents) 
    //    //    System.out.println(jdContent);
    //    //
    //    获取请求 前提需要联网
    //    //String url="https://search.jd.com/Search?keyword=java";
    //    解析网页 jsoup返回的document对象就是浏览器document对象
    //    所有在js中可以使用的方法,这里都能使用 比如document.getElementById()
    //    //Document document = Jsoup.parse(new URL(url), 30000);
    //    获取我们想要的数据
    //    //Element element = document.getElementById("J_goodsList");
    //    //System.out.println(element.html());
    //    获取全部的li元素
    //    //Elements elements = element.getElementsByTag("li");
    //    //System.out.println(elements);
    //    获取元素中的内容
    //    //for (Element el : elements) 
    //    //    //图片
    //    //    //为什么明明可以看到图片的src但是就是获取不到呢
    //    //    //这里面用到一个懒加载机制 真正的图片是放在source-data-lazy-img(data-lazy-img)中的
    //    //    String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
    //    //    //价格
    //    //    String price = el.getElementsByClass("p-price").eq(0).text();
    //    //    //标题
    //    //    String title = el.getElementsByClass("p-name").eq(0).text();
    //    //    System.out.println("-------------------------------");
    //    //    System.out.println(img);
    //    //    System.out.println(price);
    //    //    System.out.println(title);
    //    //
    //    //
    //
    //解析京东请求
    public ArrayList<JDContent> parseJD(String keywords) throws IOException 
        //获取请求 前提需要联网
        String url="https://search.jd.com/Search?keyword="+keywords;
        //解析网页 jsoup返回的document对象就是浏览器document对象
        //所有在js中可以使用的方法,这里都能使用 比如document.getElementById()
        Document document = Jsoup.parse(new URL(url), 30000);
        //获取我们想要的数据
        Element element = document.getElementById("J_goodsList");
        System.out.println(element.html());
        //获取全部的li元素
        Elements elements = element.getElementsByTag("li");
        //京东实体对象集合
        ArrayList<JDContent> jdContents = new ArrayList<>();
        //获取元素中的内容
        for (Element el : elements) 
            //图片
            //为什么明明可以看到图片的src但是就是获取不到呢
            //这里面用到一个懒加载机制 真正的图片是放在source-data-lazy-img(data-lazy-img)中的
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            //价格
            String price = el.getElementsByClass("p-price").eq(0).text();
            //标题
            String title = el.getElementsByClass("p-name").eq(0).text();
            System.out.println("-------------------------------");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
            //创建京东实体对象
            JDContent jdContent = new JDContent();
            jdContent.setImg(img);
            jdContent.setTitle(title);
            jdContent.setPrice(price);
            //把全部属性放进集合里面
            jdContents.add(jdContent);
        
        return jdContents;
    springboot整合系列

SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQSpringBoot 整合 RabbitMQSpringBoot 整合 Kafka)

[SpringBoot系列]SpringBoot如何整合SSMP

springboot怎么整合activiti

SpringBoot完成SSM整合之SpringBoot整合junit

springboot整合jedis