Day82-基于ElasticSearch的实战-仿京东搜素

2023-11-07

基于ElasticSearch的实战-仿京东搜素

1、创建springboot项目,添加相关依赖

在这里插入图片描述

2、导入相关Maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.fu</groupId>
    <artifactId>es-jd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>es-jd</name>
    <description>es-jd</description>
    <properties>
        <java.version>1.8</java.version>
        <!--定义es版本依赖,保证和本地一致,我的本地es版本为7.6.2,
        注意这里也要和SpringBoot版本对相应,不同的SpringBoot版本对应不同的es版本,否则会不兼容,已踩过坑!!! -->
        <elasticsearch.version>7.6.2</elasticsearch.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.fu.EsJdApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3、编写代码

  • config
package com.fu.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchClientConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}
  • controller层
    • ContentController
package com.fu.controller;

import com.fu.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class ContentController {
    @Autowired
    private ContentService contentService;

    @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return contentService.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 contentService.searchPage(keyword, pageNo, pageSize);
        return contentService.searchPageHighlightBuilder(keyword, pageNo, pageSize);
    }

}
  • IndexController
package com.fu.controller;

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

@Controller
public class IndexController {

    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }
}
  • service层
package com.fu.service;

import com.alibaba.fastjson.JSON;
import com.fu.entity.Content;
import com.fu.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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

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

@Service
public class ContentService {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient Client;


    // 1.解析数据放入 es索引中
    public Boolean parseContent(String keywords) throws Exception {
        List<Content> contents = new HtmlParseUtil().parseJD(keywords);

        //把查询到的数据放入es索引库中
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("5m");
        for (int i = 0; i < contents.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("jd_goods")
                            .source(JSON.toJSONString(contents.get(i)), XContentType.JSON));
        }
        BulkResponse bulk = Client.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulk.hasFailures();
    }



    // 2.获取这些数据实现搜索高亮功能
    public List<Map<String, Object>> searchPageHighlightBuilder(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }

        //条件搜索
        SearchRequest searchRequest = new SearchRequest("jd_goods");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //精准匹配
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", keyword);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.requireFieldMatch(false); //多个高亮显示
        highlightBuilder.preTags("<span style='color:red'>");//设置前缀
        highlightBuilder.postTags("</span>");                //设置后缀
        sourceBuilder.highlighter(highlightBuilder);


        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = Client.search(searchRequest, RequestOptions.DEFAULT);
        //解析结果
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {

            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField title = highlightFields.get("title");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap(); //原来的结果!
            //解析高亮的字段,将原来的字段换为我们高亮的字段即可!
            if (title != null) {
                Text[] fragments = title.fragments();
                String n_title = "";
                for (Text text : fragments) {
                    n_title += text;
                    sourceAsMap.put("title", n_title); //高亮字段替换掉原来的内容即可!
                }
                list.add(sourceAsMap);
            }
        }
        return list;
    }

}

  • utils工具类
package com.fu.utils;

import com.fu.entity.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HtmlParseUtil {

    public List<Content> parseJD(String keywords) throws Exception {
        //获取请求 https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword="+keywords+"&enc=utf-8";
        //解析网页,Jsoup返回的document就是浏览器Document对象
        Document document = Jsoup.parse(new URL(url), 30000);
//        System.out.println(document.html());
        //获取商品列表
        Element j_goodsList = document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements elements = document.getElementsByTag("li");
        //创建一个集合存储商品内容
        List<Content> contents = new ArrayList<>();
        //获取元素中的内容
        for (Element el : elements ){
            //由于图片是延迟加载
            //date-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();
//            String commit = el.getElementsByClass("p-commit").eq(0).text();
            String commit = "10万+";
            String shopnum = el.getElementsByClass("p-shop").eq(0).text();
            contents.add(new Content(img,price,title,commit,shopnum));
        }
        return contents;

    }

}

  • 实体类
package com.fu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content {
    private String img;      //图片
    private String price;    //价格
    private String title;    //标题
    private String commit;   //评价数量
    private String shopnum;  //商品编号
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Day82-基于ElasticSearch的实战-仿京东搜素 的相关文章

随机推荐

  • lightGBM介绍,以及xgboost与lightGBM的区别

    一 lightGBM 1 简介 lightGBM全称为light Gradient Boosting Machine 2017年经微软推出 是XGBoost的升级版 在大规模数据集上运行效率更高 GBDT在每一次迭代的时候 都需要遍历整个训
  • 解决虚拟机Linux获取不到ip或者登陆地址tty1获取不到ip方法

    查询不到ip虚拟机127 0 0 0 使用root登陆 us root 1 如果ifconfig命令无法使用或者查不到也可以 然后使用 dhclient v命令 1 然后ip addr 就可以了
  • 新创建多模块工程执行maven编译打包出现non-resolvable parent pom异常

    新创建多模块工程 在执行mvn clean install时抛出non resolvable parent pom 找不到父pom异常 FATAL Non resolvable parent POM for com alibaba unkn
  • 中文核心期刊与科技核心期刊区别?

    投稿前一定要先搞清楚各大期刊的区别 在我们发表论文之前 一定要先搞清楚各大期刊的区别进行精准投递 由于各期刊的等级分类不同 评选组织不同 评选范围和评选的领域不同 在选择的时候 一定要搞清楚各期刊的类别划分 不仅可以帮助我们精准定位自己的学
  • C++11智能指针之unique_ptr

    1 智能指针概念 智能指针是基于RAII机制实现的类 模板 具有指针的行为 重载了operator 与operator gt 操作符 可以 智能 地销毁其所指对象 C 11中有unique ptr shared ptr与weak ptr等智
  • 闲鱼项目玩法实战,月入破万实战指南!

    一些闲鱼的实操技巧玩法 做短期项目都同学一定收藏下 1 相同属性的商品上架1 2个即可不宜过多 每天上新1 2为宜 过多都会限流 2 商品名称根据用户可能会输入的关键词去拆分下逐个输入搜索框 如果有搜不到的 就是谐词汇 不断尝试更换 直到都
  • ESP32 S3-OLED显示小数函数

    ESP32 S3 ardino平台 配中景园7针0 96OLED屏显示小数 OLED网上的驱动代码一般厂商发货会提供驱动程序 但是显示小数很多都没有编写 这里编写了一段可显示任意位小数的代码 以正点原子代码为基础 需要显示有符号的小数程序稍
  • 云原生全栈体系(二)

    Kubernetes实战入门 第一章 Kubernetes基础概念 一 是什么 我们急需一个大规模容器编排系统 kubernetes具有以下特性 服务发现和负载均衡 Kubernetes 可以使用 DNS 名称或自己的 IP 地址公开容器
  • 《自己动手设计数据库》第7章 设计表结构

    第7章 建立表结构 到此章为止 你手上应该有3张列表了 主题列表 经过评审的特征列表 又名初始字段列表 计算字段列表 定义初始表列表 要执行这一段内容 还需要第5章定义的任务目标 确定隐含主题 首先不去看主题列表 这里先去看初始字段列表 原
  • 执行程序时提示cuBLAS Error: cublasGemmStridedBatchedEx failed.

    操作步骤 问题现象 在使用Mindspore GPU跑程序的时候出现报错 CRITICAL KERNEL 1084 7f5e5ffff700 python3 2022 06 22 19 46 23 385 199 mindspore ccs
  • linux查看文件夹大小

    linux怎么查看文件夹多大 1 最简单的查看方法可以使用ls ll ls lh命令进行查看 当使用ls ll 会显示成字节大小 而ls lh会以KB MB等为单位进行显示 这样比较直观一些 2 通过命令du h max depth 1 可
  • Lesson 6.4 逻辑回归手动调参实验

    文章目录 一 数据准备与评估器构造 1 数据准备 2 构建机器学习流 二 评估器训练与过拟合实验 三 评估器的手动调参 在补充了一系列关于正则化的基础理论以及 sklearn 中逻辑回归评估器的参数解释之后 接下来 我们尝试借助 sklea
  • 知网CAJ直接PDF下载,并且autoBookMark添加书目

    知网能下PDF 谷歌浏览器下 You猴 安装这两个插件 CNKI 中国知网 PDF 全文下载 特制版 知网下载助手 安装所需要的文件链接 链接 https pan baidu com s 1sIKJnvuZE2P8r4HSETrWGQ 提取
  • Android TextEdit 文本框设置

    文本框类型设置 android inputType none 输入普通字符 android inputType text 输入普通字符 android inputType textCapCharacters 输入普通字符 android i
  • 从零开始学习React——(十六):利用React生命周期优化组件

    通过上一节 对于React生命周期有了新认识 如何利用它来提高组件的性能呢 本节将会抛砖引玉讲解一个小例子 为了代码的清晰 可以删除上一节一些无关的生命周期函数代码 1 ChildItem js存在性能问题 那就是 子组件ChildItem
  • GPU指令下发方式

    1 概述 GPU接收CPU发送的渲染命令 执行相应的计算 渲染命令在CPU和GPU之间传递 由CPU发送给GPU AMD的GPU有两种命令发送方式 第一种是CPU通过直接写GPU的寄存器 发送相应的渲染命令 对于GPU来说 这种方式是CPU
  • Vue中table实现行的上移和下移

    链接 html
  • 【基础知识学习】链表的创建

    链表的创建 因为对链表使用不太熟悉 学习使用数组创建了一个链表并访问 代码如下 以后学到新的知识继续补充 include
  • mfc入门基础(七)向导对话框的创建与显示

    实现参考 VS2010 MFC编程入门之十四 对话框 向导对话框的创建及显示 软件开发 鸡啄米 一 向导对话框的创建与显示 1 具体的例子使用 还是参照上节或者说上上节中的例子写出来 test02 所以也还是在这个基础上来进行更改 2 创建
  • Day82-基于ElasticSearch的实战-仿京东搜素

    基于ElasticSearch的实战 仿京东搜素 1 创建springboot项目 添加相关依赖 2 导入相关Maven依赖