Spring Boot整合ElasticSearch和Mysql 附案例源码

导读#

  前二天,写了一篇 ElasticSearch7.8.1 从入门到精通的(点我直达),但是还没有整合到 SpringBoot 中,下面演示将 ElasticSearch 和 mysql 整合到 Spring Boot 中,附演示源码。

项目介绍#

模仿 NBA 网站#

网址地址:点我直达

 

接口开发#

  1. 将数据库数据导入到 ElasticSearch
  2. 通过姓名查找球员
  3. 通过国家或者球队查询球员
  4. 通过姓名字母查找球员

项目搭建#

SpringBoot 整合 ElasticSearch 和 Mysql#

数据库数据#

  将百度云盘里的 sql,在 mysql 上运行即可

链接: https://pan.baidu.com/s/1MJaJy8isfVnPha00tlS8_w  密码: u3dg

项目结构#

pom.xml#

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cyb</groupId>
    <artifactId>yb_search</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>yb_search</name>
    <description>Demo project for Spring Boot</description>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">java</span><span style="color: rgba(255, 0, 0, 1)">.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.8<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">java.version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-web<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">ElasticSearch相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.elasticsearch.client<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>elasticsearch-rest-high-level-client<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>7.8.1<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.elasticsearch<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>elasticsearch<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>7.8.1<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">ElasticSearch相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">fastjson相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.alibaba<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>fastjson<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.2.73<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">fastjson相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">druid相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.alibaba<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>druid<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.1.23<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">druid相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">mybatis与springboot兼容包相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.mybatis.spring.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mybatis-spring-boot-starter<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.1.3<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">mybatis与springboot兼容包相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">mysql相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>8.0.21<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>8.0.21<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">mysql相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">commons-lang3相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.commons<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>commons-lang3<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.11<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">commons-lang3相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</project>

注意

  我本地安装的 ElasticSearch 版本是 7.8.1,引入 pom 的 ES 依赖的话,最好版本一致,否则可能出现版本兼容问题!!!!!!!!!!!

application.properties#

# 端口号
server.port=8083
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
# 连接池
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 表明使用 Druid 连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#初始化时建立物理连接的个数。
spring.datasource.druid.initial-size=5
#最大连接池数量
spring.datasource.druid.max-active=20
#最小连接池数量
spring.datasource.druid.min-idle=5
#获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=3000
#是否缓存 preparedStatement,也就是 PSCache,PSCache 对支持游标的数据库性能提升巨大,比如说 oracle, 在 mysql 下建议关闭。
spring.datasource.druid.pool-prepared-statements=false
#要启用 PSCache,必须配置大于 0,当大于 0 时,poolPreparedStatements 自动触发修改为 true。在 Druid 中,不会存在 Oracle 下 PSCache 占用内存过多的问题,可以把这个数值配置大一些,比如说 100
spring.datasource.druid.max-open-prepared-statements= -1
#配置检测可以关闭的空闲连接间隔时间
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 配置连接在池中的最小生存时间
spring.datasource.druid.min-evictable-idle-time-millis= 300000
spring.datasource.druid.max-evictable-idle-time-millis= 400000
# 日志相关
logging.level.root: info
logging.level.com.cyb.search: debug
# ElasticSearch 配置
elasticsearch.host:192.168.199.170
elasticsearch.port=9200

EsConfig.java(ES 配置类)#

package com.cyb.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**

  • @ClassName:EsConfig

  • @Description:ES 配置文件

  • @Author:chenyb

  • @Date:2020/8/10 11:25 下午

  • @Versiion:1.0
    */
    @Configuration
    //获取 application.properties 或 application.yml 获取里面的参数值
    @ConfigurationProperties(prefix = "elasticsearch")
    public class EsConfig {
    private String host;
    private Integer port;
    //初始化 RestHighLevelClient
    @Bean(destroyMethod = "close")
    public RestHighLevelClient client(){
    return new RestHighLevelClient(RestClient.builder(
    new HttpHost(host,port,"http")
    ));
    }

    public String getHost() {
    return host;
    }

    public void setHost(String host) {
    this.host = host;
    }

    public Integer getPort() {
    return port;
    }

    public void setPort(Integer port) {
    this.port = port;
    }
    }

NBAPlayerDao.java#

package com.cyb.search.dao;

import com.cyb.search.model.NBAPlayer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface NBAPlayerDao {
@Select(
"select * from nba_player")
List
<NBAPlayer> selectAll();
}

NBAPlayer.java(实体类)#

package com.cyb.search.model;

/**

  • @ClassName:NBAPlayer

  • @Description:TODO

  • @Author:chenyb

  • @Date:2020/8/10 11:39 下午

  • @Versiion:1.0
    */
    public class NBAPlayer {
    private Integer id;
    private String countryEn;
    private String country;
    private String code;
    private String displayAffiliation;
    private String displayName;
    private Integer draft;
    private String schoolType;
    private String weight;
    private Integer playYear;
    private String jerseyNo;
    private Long birthDay;
    private String birthDayStr;
    private String displayNameEn;
    private String position;
    private Double heightValue;
    private String playerId;
    private String teamCity;
    private String teamCityEn;
    private String teamName;
    private String teamNameEn;
    private String teamConference;
    private String teamConferenceEn;
    private Integer age;
    public String getCountry() {
    return country;
    }

    public void setCountry(String country) {
    this.country = country;
    }

    public String getBirthDayStr() {
    return birthDayStr;
    }

    public void setBirthDayStr(String birthDayStr) {
    this.birthDayStr = birthDayStr;
    }

    public Integer getAge() {
    return age;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public String getDisplayAffiliation() {
    return displayAffiliation;
    }

    public void setDisplayAffiliation(String displayAffiliation) {
    this.displayAffiliation = displayAffiliation;
    }

    public String getDisplayName() {
    return displayName;
    }

    public void setDisplayName(String displayName) {
    this.displayName = displayName;
    }

    public Integer getDraft() {
    return draft;
    }

    public void setDraft(Integer draft) {
    this.draft = draft;
    }

    public String getSchoolType() {
    return schoolType;
    }

    public void setSchoolType(String schoolType) {
    this.schoolType = schoolType;
    }

    public String getWeight() {
    return weight;
    }

    public void setWeight(String weight) {
    this.weight = weight;
    }

    public Integer getPlayYear() {
    return playYear;
    }

    public void setPlayYear(Integer playYear) {
    this.playYear = playYear;
    }

    public String getCountryEn() {
    return countryEn;
    }

    public void setCountryEn(String countryEn) {
    this.countryEn = countryEn;
    }

    public String getTeamCityEn() {
    return teamCityEn;
    }

    public void setTeamCityEn(String teamCityEn) {
    this.teamCityEn = teamCityEn;
    }

    public String getTeamNameEn() {
    return teamNameEn;
    }

    public void setTeamNameEn(String teamNameEn) {
    this.teamNameEn = teamNameEn;
    }

    public String getTeamConference() {
    return teamConference;
    }

    public void setTeamConference(String teamConference) {
    this.teamConference = teamConference;
    }

    public String getTeamConferenceEn() {
    return teamConferenceEn;
    }

    public void setTeamConferenceEn(String teamConferenceEn) {
    this.teamConferenceEn = teamConferenceEn;
    }

    public String getJerseyNo() {
    return jerseyNo;
    }

    public void setJerseyNo(String jerseyNo) {
    this.jerseyNo = jerseyNo;
    }

    public Long getBirthDay() {
    return birthDay;
    }

    public void setBirthDay(Long birthDay) {
    this.birthDay = birthDay;
    }

    public String getDisplayNameEn() {
    return displayNameEn;
    }

    public void setDisplayNameEn(String displayNameEn) {
    this.displayNameEn = displayNameEn;
    }

    public String getPosition() {
    return position;
    }

    public void setPosition(String position) {
    this.position = position;
    }

    public Double getHeightValue() {
    return heightValue;
    }

    public void setHeightValue(Double heightValue) {
    this.heightValue = heightValue;
    }

    public String getPlayerId() {
    return playerId;
    }

    public void setPlayerId(String playerId) {
    this.playerId = playerId;
    }

    public String getTeamCity() {
    return teamCity;
    }

    public void setTeamCity(String teamCity) {
    this.teamCity = teamCity;
    }

    public String getTeamName() {
    return teamName;
    }

    public void setTeamName(String teamName) {
    this.teamName = teamName;
    }

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }
    }

NBAPlayerService.java(接口)#

package com.cyb.search.service;

import com.cyb.search.model.NBAPlayer;

import java.io.IOException;

public interface NBAPlayerService {
boolean addPlayer(NBAPlayer player,String id) throws IOException;
}

NBAPlayerServiceImpl.java

package com.cyb.search.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cyb.search.model.NBAPlayer;
import com.cyb.search.service.NBAPlayerService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

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

/**

  • @ClassName:NBAPlayerServiceImpl

  • @Description:TODO

  • @Author:chenyb

  • @Date:2020/8/11 10:09 下午

  • @Versiion:1.0
    */
    @Service
    public class NBAPlayerServiceImpl implements NBAPlayerService {
    @Resource
    private RestHighLevelClient client;

    /**

    • 添加
    • @param player 实体类
    • @param id 编号
    • @return
    • @throws IOException
      */
      @Override
      public boolean addPlayer(NBAPlayer player, String id) throws IOException {
      IndexRequest request
      =new IndexRequest("nba_latest").id(id).source(beanToMap(player));
      IndexResponse response
      = client.index(request, RequestOptions.DEFAULT);
      System.out.println(JSONObject.toJSON(response));
      return false;
      }

    /**

    • 对象转 map
    • @param bean
    • @param <T>
    • @return
      */
      public static <T> Map<String,Object> beanToMap(T bean){
      Map
      <String,Object> map=new HashMap<>();
      if (bean!=null){
      BeanMap beanMap
      =BeanMap.create(bean);
      for(Object key:beanMap.keySet()){
      if (beanMap.get(key)!=null){
      map.put(key
      +"",beanMap.get(key));
      }
      }
      }
      return map;
      }
      }

基础功能实现#

往 ES 中插入一条数据#

 

 

查看数据库数据#

 

 

根据 ID 查 ES #

 

 

单元测试

 

修改#

 

 

单元测试

删除#

 

 

单元测试

将数据库中的数据导入 ES#

 

 

通过名字查找球员#

 

 

测试#

 

通过国家或球队查找球员#

 

 

测试#

 

 

通过字母查球员#

 

 

测试#

 

项目源码下载#

链接: https://pan.baidu.com/s/1QJ8wvjg7TPqGSP-68qpSIQ  密码: d26m

SpringBoot 整合 ES 优化通用工具类 二#

依赖项#

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ybchen</groupId>
    <artifactId>ybchen-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ybchen-es</name>
    <description>SpringBoot 整合 ES</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.projectlombok<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>lombok<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-starter-test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.baomidou<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mybatis-plus-boot-starter<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.4.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 代码自动生成依赖 begin </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.baomidou<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mybatis-plus-generator<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.4.1<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> velocity </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.apache.velocity<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>velocity-engine-core<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 代码自动生成依赖 end</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">springBoot整合swagger3.0</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>io.springfox<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>springfox-boot-starter<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.0.0<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">ElasticSearch相关开始</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.elasticsearch.client<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>elasticsearch-rest-high-level-client<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>7.6.2<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.elasticsearch<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>elasticsearch<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>7.6.2<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">ElasticSearch相关结束</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.alibaba.fastjson2<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>fastjson2<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>2.0.18<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.springframework.boot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>spring-boot-maven-plugin<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">configuration</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">excludes</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">exclude</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.projectlombok<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>lombok<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">exclude</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
                <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">excludes</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
            <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">configuration</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugin</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">build</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</project>

pom.xml

配置文件#

spring.application.name=es-service
# ===== 自定义 swagger 配置 ===== #
swagger.enable=true
swagger.application-name= ${spring.application.name}
swagger.application-version=1.0
swagger.application-description=es api
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis-plus 配置控制台打印完整带参数 SQL 语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# ElasticSearch 配置
elasticsearch.host=47.116.143.16
elasticsearch.port=9200
elasticsearch.userName=es
elasticsearch.password=es

ES 客户端#

package com.ybchen.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**

  • es 客户端

  • @author: chenyanbin 2022-11-06 14:08
    */
    @Service
    @Slf4j
    public class EsService {
    @Value(
    "${elasticsearch.host}")
    String esHosts;
    @Value(
    "${elasticsearch.port}")
    int esPort;
    @Value(
    "${elasticsearch.userName}")
    String esUserName;
    @Value(
    "${elasticsearch.password}")
    String esPassword;

    private static volatile RestHighLevelClient instance;

    /**

    • 获取 es 客户端
    • @return
      */
      public RestHighLevelClient getInstance() {
      if (instance == null) {
      synchronized (EsService.class) {
      if (instance == null) {
      instance
      = createInstance();
      }
      }
      }
      return instance;
      }

    /**

    • 创建 es 客户端
    • @return
      */
      private RestHighLevelClient createInstance() {
      List
      <HttpHost> httpHostsList = new ArrayList<>();
      //填充 es 数据
      httpHostsList.add(new HttpHost(esHosts, esPort));
      //es 客户端构建者
      RestClientBuilder clientBuilder = RestClient.builder(httpHostsList.get(0));
      //异步链接延时配置
      clientBuilder.setRequestConfigCallback(requestConfigBuilder ->
      requestConfigBuilder
      .setConnectTimeout(
      5000) //5 秒
      .setSocketTimeout(5000)
      .setConnectionRequestTimeout(
      5000)
      );
      //异步链接数配置
      clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
      //最大连接数 100 个
      httpClientBuilder.setMaxConnTotal(100);
      //最大路由连接数
      httpClientBuilder.setMaxConnPerRoute(100);
      //==================ES 有账号密码 - 开始 ======================
      final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
      credentialsProvider.setCredentials(AuthScope.ANY,
      new UsernamePasswordCredentials(esUserName, esPassword));
      httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      //==================ES 有账号密码 - 结束 ======================
      // 设置 KeepAlive 为 5 分钟的时间,不设置默认为 -1,也就是持续连接,然而这会受到外界的影响比如 Firewall,会将 TCP 连接单方面断开,从而会导致 Connection reset by peer 的报错
      // 参考 github 解决方案:https://github.com/TFdream/Elasticsearch-learning/issues/30
      httpClientBuilder.setKeepAliveStrategy((response, context) -> TimeUnit.MINUTES.toMillis(3))
      .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(
      1).setSoKeepAlive(true).build());
      return httpClientBuilder;
      });
      return new RestHighLevelClient(clientBuilder);
      }

    /**

    • 30 秒一次检查 es 状态
      */
      @Scheduled(fixedRate
      = 30 * 1000)
      public void heartbeatToES() {
      try {
      RequestOptions requestOptions
      = RequestOptions.DEFAULT.toBuilder().build();
      boolean result = getInstance().ping(requestOptions);
      log.info(
      "检查 ES 状态:{}", result);
      }
      catch (Exception e) {
      log.error(
      "检查 ES 状态发生异常:{}", e);
      }
      }
      }
EsService.java

工具类#

package com.ybchen.service;

import com.ybchen.model.NbaPlayerDO;
import com.ybchen.vo.EsDocumentVo;

import java.util.List;
import java.util.Map;

public interface NbaService {
/**
* 添加文档
*
*
@param indexName 索引名称
*
@param docId 文档 id
*
@param jsonBody json 数据
*
@return
*/
boolean addDoc(
String indexName,
String docId,
String jsonBody
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 异步添加文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName 索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> docId     文档id
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jsonBody  json数据
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> asyncAddDoc(
        String indexName,
        String docId,
        String jsonBody
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 批量添加文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName      索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> documentVoList 文档对象集合
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> batchAddDoc(
        String indexName,
        List</span>&lt;EsDocumentVo&gt;<span style="color: rgba(0, 0, 0, 1)"> documentVoList
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 批量异步添加文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName      索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> documentVoList 文档对象集合
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> batchAsyncAddDoc(
        String indexName,
        List</span>&lt;EsDocumentVo&gt;<span style="color: rgba(0, 0, 0, 1)"> documentVoList
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据文档id搜索文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName 索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> docId     文档id
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
Map</span>&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)"> getDocumentByDocId(
        String indexName,
        String docId
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据文档id更新文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName 索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> docId     文档id
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jsonBody  json数据
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> updateDocumentByDocId(
        String indexName,
        String docId,
        String jsonBody
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 批量异步根据文档id更新文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName      索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> documentVoList 文档对象集合
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> batchAsyncUpdateDocumentByDocId(
        String indexName,
        List</span>&lt;EsDocumentVo&gt;<span style="color: rgba(0, 0, 0, 1)"> documentVoList
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据文档id删除文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName 索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> docId     文档id
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> deleteDocumentByDocId(
        String indexName,
        String docId
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据索引名称全部删除文档
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName 索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> deleteAllDocumentByIndexName(
        String indexName
);

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 通过名字查找球员
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> indexName      索引名称
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> key            文档字段
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> value          文档值
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> pageNum        第几页
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> pageSize       一页多少条
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> highlightField 高亮字段
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
List</span>&lt;NbaPlayerDO&gt;<span style="color: rgba(0, 0, 0, 1)"> searchMatch(
        String indexName,
        String key,
        String value,
        </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageNum,
        </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> pageSize,
        String highlightField
);

}

NbaService.java
package com.ybchen.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ybchen.model.NbaPlayerDO;
import com.ybchen.service.EsService;
import com.ybchen.service.NbaService;
import com.ybchen.vo.EsDocumentVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
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.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**

  • @author: chenyanbin 2022-11-06 14:36
    */
    @Service
    @Slf4j
    public class NbaServiceImpl implements NbaService {
    @Autowired
    EsService esService;

    /**

    • 添加文档
    • @param indexName 索引名称
    • @param docId 文档 id
    • @param jsonBody json 数据
    • @return
      */
      @Override
      public boolean addDoc(
      String indexName,
      String docId,
      String jsonBody
      ) {
      IndexRequest indexRequest
      = new IndexRequest();
      //添加索引名称
      indexRequest.index(indexName);
      //文档 id
      indexRequest.id(docId);
      //json 数据
      indexRequest.source(jsonBody, XContentType.JSON);
      try {
      //添加文档
      esService.getInstance().index(
      indexRequest,
      RequestOptions.DEFAULT
      );
      log.info(
      "添加文档成功,indexName:{},docId:{},jsonBody:{}",
      indexName,
      docId,
      jsonBody
      );
      return true;
      }
      catch (Exception e) {
      log.error(
      "添加文档失败,indexName:{},docId:{},jsonBody:{},异常信息:{}",
      indexName,
      docId,
      jsonBody,
      e
      );
      }
      return false;
      }

    /**

    • 异步添加文档

    • @param indexName 索引名称

    • @param docId 文档 id

    • @param jsonBody json 数据

    • @return
      */
      @Override
      public boolean asyncAddDoc(
      String indexName,
      String docId,
      String jsonBody
      ) {
      IndexRequest indexRequest
      = new IndexRequest();
      //添加索引名称
      indexRequest.index(indexName);
      //文档 id
      indexRequest.id(docId);
      //json 数据
      indexRequest.source(jsonBody, XContentType.JSON);
      //异步批量插入
      esService.getInstance().indexAsync(
      indexRequest,
      RequestOptions.DEFAULT,
      new ActionListener<IndexResponse>() {
      @Override
      public void onResponse(IndexResponse indexResponse) {
      log.info(
      "异步添加文档成功,indexName:{},docId:{},jsonBody:{}",
      indexName,
      docId,
      jsonBody
      );
      }

               @Override
               </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onFailure(Exception e) {
                   log.error(</span>"异步添加文档失败,indexName:{},docId:{},jsonBody:{},异常信息:{}"<span style="color: rgba(0, 0, 0, 1)">,
                           indexName,
                           docId,
                           jsonBody,
                           e
                   );
               }
           });
      

      return true;
      }

    /**

    • 批量添加文档
    • @param indexName 索引名称
    • @param documentVoList 文档对象集合
    • @return
      */
      @Override
      public boolean batchAddDoc(
      String indexName,
      List
      <EsDocumentVo> documentVoList
      ) {
      if (documentVoList == null || documentVoList.size() == 0) {
      return false;
      }
      //批量文档请求对象
      BulkRequest bulkRequest = new BulkRequest();
      for (EsDocumentVo esDocumentVo : documentVoList) {
      IndexRequest indexRequest
      = new IndexRequest();
      //添加索引名称
      indexRequest.index(indexName);
      //文档 id
      indexRequest.id(esDocumentVo.getDocId());
      //json 数据
      indexRequest.source(esDocumentVo.getJsonBody(), XContentType.JSON);
      bulkRequest.add(indexRequest);
      }
      try {
      //批量添加
      esService.getInstance().bulk(bulkRequest, RequestOptions.DEFAULT);
      log.info(
      "批量添加文档成功,indexName:{},documentVoList:{}",
      indexName,
      documentVoList
      );
      return true;
      }
      catch (Exception e) {
      log.error(
      "批量添加文档失败,indexName:{},documentVoList:{},异常信息:{}",
      indexName,
      documentVoList,
      e
      );
      }
      return false;
      }

    /**

    • 批量异步添加文档

    • @param indexName 索引名称

    • @param documentVoList 文档对象集合

    • @return
      */
      @Override
      public boolean batchAsyncAddDoc(
      String indexName,
      List
      <EsDocumentVo> documentVoList
      ) {
      if (documentVoList == null || documentVoList.size() == 0) {
      return false;
      }
      //批量文档请求对象
      BulkRequest bulkRequest = new BulkRequest();
      for (EsDocumentVo esDocumentVo : documentVoList) {
      IndexRequest indexRequest
      = new IndexRequest();
      //添加索引名称
      indexRequest.index(indexName);
      //文档 id
      indexRequest.id(esDocumentVo.getDocId());
      //json 数据
      indexRequest.source(esDocumentVo.getJsonBody(), XContentType.JSON);
      bulkRequest.add(indexRequest);
      }
      //批量添加
      esService.getInstance().bulkAsync(bulkRequest, RequestOptions.DEFAULT, new ActionListener<BulkResponse>() {
      @Override
      public void onResponse(BulkResponse bulkItemResponses) {
      log.info(
      "批量异步添加文档成功,indexName:{},documentVoList:{}",
      indexName,
      documentVoList
      );
      }

       @Override
       </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onFailure(Exception e) {
           log.info(</span>"批量异步添加文档失败,indexName:{},documentVoList:{},异常信息:{}"<span style="color: rgba(0, 0, 0, 1)">,
                   indexName,
                   documentVoList,
                   e
           );
       }
      

      });
      return true;
      }

    /**

    • 根据文档 id 搜索文档
    • @param indexName 索引名称
    • @param docId 文档 id
    • @return
      */
      @Override
      public Map<String, Object> getDocumentByDocId(
      String indexName,
      String docId
      ) {
      GetRequest getRequest
      = new GetRequest();
      getRequest.index(indexName);
      getRequest.id(docId);
      try {
      //查询
      GetResponse response = esService.getInstance().get(getRequest, RequestOptions.DEFAULT);
      return response.getSource();
      }
      catch (Exception e) {
      log.error(
      "根据文档 id 搜索文档失败,indexName:{},docId:{},异常信息:{}",
      indexName,
      docId,
      e
      );
      }
      return null;
      }

    /**

    • 根据文档 id 更新文档
    • @param indexName 索引名称
    • @param docId 文档 id
    • @param jsonBody json 数据
    • @return
      */
      @Override
      public boolean updateDocumentByDocId(
      String indexName,
      String docId,
      String jsonBody
      ) {
      UpdateRequest updateRequest
      = new UpdateRequest();
      //添加索引名称
      updateRequest.index(indexName);
      //文档 id
      updateRequest.id(docId);
      //json 数据
      updateRequest.doc(jsonBody, XContentType.JSON);
      try {
      //更新文档
      esService.getInstance().update(updateRequest, RequestOptions.DEFAULT);
      log.error(
      "根据文档 id 更新文档成功,indexName:{},docId:{},jsonBody:{}",
      indexName,
      docId,
      jsonBody
      );
      return true;
      }
      catch (Exception e) {
      log.error(
      "根据文档 id 更新文档失败,indexName:{},docId:{},jsonBody:{},异常信息:{}",
      indexName,
      docId,
      jsonBody,
      e
      );
      }
      return false;
      }

    /**

    • 批量异步根据文档 id 更新文档

    • @param indexName 索引名称

    • @param documentVoList 文档对象集合

    • @return
      */
      @Override
      public boolean batchAsyncUpdateDocumentByDocId(
      String indexName,
      List
      <EsDocumentVo> documentVoList
      ) {
      if (documentVoList == null || documentVoList.size() == 0) {
      return false;
      }
      BulkRequest bulkRequest
      = new BulkRequest();
      for (EsDocumentVo esDocumentVo : documentVoList) {
      UpdateRequest updateRequest
      = new UpdateRequest();
      //添加索引名称
      updateRequest.index(indexName);
      //文档 id
      updateRequest.id(esDocumentVo.getDocId());
      //json 数据
      updateRequest.doc(esDocumentVo.getJsonBody(), XContentType.JSON);
      bulkRequest.add(updateRequest);
      }
      esService.getInstance().bulkAsync(bulkRequest, RequestOptions.DEFAULT,
      new ActionListener<BulkResponse>() {
      @Override
      public void onResponse(BulkResponse bulkItemResponses) {
      log.info(
      "批量异步根据文档 id 更新文档成功,indexName:{},documentVoList:{}",
      indexName,
      documentVoList
      );
      }

       @Override
       </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onFailure(Exception e) {
           log.info(</span>"批量异步根据文档id更新文档失败,indexName:{},documentVoList:{},异常信息:{}"<span style="color: rgba(0, 0, 0, 1)">,
                   indexName,
                   documentVoList,
                   e
           );
       }
      

      });
      return true;
      }

    /**

    • 根据文档 id 删除文档
    • @param indexName 索引名称
    • @param docId 文档 id
    • @return
      */
      @Override
      public boolean deleteDocumentByDocId(String indexName, String docId) {
      DeleteRequest deleteRequest
      = new DeleteRequest();
      //添加索引名称
      deleteRequest.index(indexName);
      //文档 id
      deleteRequest.id(docId);
      try {
      esService.getInstance().delete(deleteRequest, RequestOptions.DEFAULT);
      log.error(
      "根据文档 id 删除文档成功,indexName:{},docId:{}",
      indexName,
      docId
      );
      return true;
      }
      catch (Exception e) {
      log.error(
      "根据文档 id 删除文档失败,indexName:{},docId:{},异常信息:{}",
      indexName,
      docId,
      e
      );
      }
      return false;
      }

    /**

    • 根据索引名称全部删除文档
    • @param indexName 索引名称
    • @return
      */
      @Override
      public boolean deleteAllDocumentByIndexName(String indexName) {
      DeleteIndexRequest deleteIndexRequest
      = new DeleteIndexRequest();
      deleteIndexRequest.indices(indexName);
      try {
      esService.getInstance().indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
      log.error(
      "根据索引名称全部删除文档成功,indexName:{}",
      indexName
      );
      return true;
      }
      catch (Exception e) {
      log.error(
      "根据索引名称全部删除文档失败,indexName:{},异常信息:{}",
      indexName,
      e
      );
      }
      return false;
      }

    /**

    • 通过名字查找球员
    • @param indexName 索引名称
    • @param key 文档字段
    • @param value 文档值
    • @param pageNum 第几页
    • @param pageSize 一页多少条
    • @param highlightField 高亮字段
    • @return
      */
      @Override
      public List<NbaPlayerDO> searchMatch(
      String indexName,
      String key,
      String value,
      int pageNum,
      int pageSize,
      String highlightField
      ) {
      //>>>>>> 构建查找对象
      SearchRequest searchRequest = new SearchRequest();
      //添加索引名称
      searchRequest.indices(indexName);
      //>>>>>>> 类似于查询语句,相当于:where 后面的条件
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      //构建查询条件 displayname:查询字段名称
      searchSourceBuilder.query(QueryBuilders.matchQuery(key, value));
      //从第 0 条开始
      searchSourceBuilder.from((pageNum - 1) * pageSize);
      //查 1000 条记录
      searchSourceBuilder.size(pageSize);
      //>>>>>>>>> 设置高亮样式
      HighlightBuilder highlightBuilder = new HighlightBuilder();
      //设置高亮字段
      highlightBuilder.field(new HighlightBuilder.Field(highlightField));
      //高亮样式,前缀样式;后缀样式
      highlightBuilder.preTags("<span style=color:red>").postTags("</span>");
      searchSourceBuilder.highlighter(highlightBuilder);
      //>>>>>> 将筛选条件添加到查找对象中
      searchRequest.source(searchSourceBuilder);
      try {
      SearchResponse response
      = esService.getInstance().search(searchRequest, RequestOptions.DEFAULT);
      //总共查询出来的数据总条数
      long total = response.getHits().getTotalHits().value;
      //本次数据查询条数
      int length = response.getHits().getHits().length;
      //拿到数据
      SearchHit[] hits = response.getHits().getHits();
      List
      <NbaPlayerDO> nbaPlayerDOList = new ArrayList<>();
      for (SearchHit hit : hits) {
      //单条记录
      Map<String, Object> sourceAsMap = hit.getSourceAsMap();
      //高亮字段
      Map<String, HighlightField> highlightFields = hit.getHighlightFields();
      HighlightField hField
      = highlightFields.get(highlightField);
      //>>>>>>> 重新组装高亮字段和值
      if (hField != null) {
      StringBuilder sbValue
      = new StringBuilder();
      for (Text text : hField.fragments()) {
      sbValue.append(text);
      }
      sourceAsMap.put(highlightField, sbValue.toString());
      }
      nbaPlayerDOList.add(JSON.parseObject(JSON.toJSONString(sourceAsMap), NbaPlayerDO.
      class));
      }
      return nbaPlayerDOList;
      }
      catch (Exception e) {
      log.error(
      "搜索发生了异常:{}", e);
      }
      return new ArrayList<>();
      }

}

NbaServiceImpl.java
package com.ybchen.vo;

import lombok.Data;

import java.io.Serializable;

/**

  • ES 文档 vo

  • @author: chenyanbin 2022-11-07 22:10
    */
    @Data
    public class EsDocumentVo implements Serializable {
    /**

    • 文档 id
      */
      private String docId;

    /**

    • 文档数据
      */
      private String jsonBody;
      }

实体类#

package com.ybchen.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;

/**

  • <p>

  • </p>

  • @author chenyanbin

  • @since 2022-11-06
    */
    @Data
    @EqualsAndHashCode(callSuper
    = false)
    @TableName(
    "nba_player")
    @ApiModel(value
    = "NbaPlayerDO 对象", description = "")
    public class NbaPlayerDO implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("countryEn")
    private String countryen;

    @TableField("teamName")
    private String teamname;

    @TableField("birthDay")
    private Long birthday;

    private String country;

    @TableField("teamCityEn")
    private String teamcityen;

    private String code;

    @TableField("displayAffiliation")
    private String displayaffiliation;

    @TableField("displayName")
    private String displayname;

    @TableField("schoolType")
    private String schooltype;

    @TableField("teamConference")
    private String teamconference;

    @TableField("teamConferenceEn")
    private String teamconferenceen;

    private String weight;

    @TableField("teamCity")
    private String teamcity;

    @TableField("playYear")
    private Integer playyear;

    @TableField("jerseyNo")
    private String jerseyno;

    @TableField("teamNameEn")
    private String teamnameen;

    private Integer draft;

    @TableField("displayNameEn")
    private String displaynameen;

    @TableField("birthDayStr")
    private String birthdaystr;

    @TableField("heightValue")
    private BigDecimal heightvalue;

    private String position;

    private Integer age;

    @TableField("playerId")
    private String playerid;

}

NbaPlayerDO.java

Mapper#

package com.ybchen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ybchen.model.NbaPlayerDO;

/**

  • <p>
  • Mapper 接口
  • </p>
  • @author chenyanbin
  • @since 2022-11-06
    */
    public interface NbaPlayerMapper extends BaseMapper<NbaPlayerDO> {

}

单元测试#

package com.ybchen.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ybchen.EsApplication;
import com.ybchen.mapper.NbaPlayerMapper;
import com.ybchen.model.NbaPlayerDO;
import com.ybchen.service.NbaService;
import com.ybchen.vo.EsDocumentVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = EsApplication.class)
class NbaServiceImplTest {
@Autowired
NbaService nbaService;
@Autowired
NbaPlayerMapper nbaPlayerMapper;

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String NBA_INDEX_NAME = "nba_player"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 添加文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addDoc() {
    NbaPlayerDO nbaPlayerDO </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
    nbaPlayerDO.setCountryen(</span>"中国"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag = nbaService.addDoc(NBA_INDEX_NAME, "188"<span style="color: rgba(0, 0, 0, 1)">, JSON.toJSONString(nbaPlayerDO));
    System.err.println(flag);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 异步添加文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> asyncAddDoc() {
    NbaPlayerDO nbaPlayerDO </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
    nbaPlayerDO.setCountryen(</span>"美国"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag = nbaService.asyncAddDoc(NBA_INDEX_NAME, "191"<span style="color: rgba(0, 0, 0, 1)">, JSON.toJSONString(nbaPlayerDO));
    System.err.println(flag);
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) {

    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 批量添加文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> batchAddDoc() {
    List</span>&lt;EsDocumentVo&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;EsDocumentVo&gt;<span style="color: rgba(0, 0, 0, 1)">() {
        {
            EsDocumentVo vo1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> EsDocumentVo();
            vo1.setDocId(</span>"192"<span style="color: rgba(0, 0, 0, 1)">);
            NbaPlayerDO nbaPlayerDO1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
            nbaPlayerDO1.setCountryen(</span>"中国"<span style="color: rgba(0, 0, 0, 1)">);
            vo1.setJsonBody(JSON.toJSONString(nbaPlayerDO1));
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(vo1);
            EsDocumentVo vo2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> EsDocumentVo();
            vo1.setDocId(</span>"193"<span style="color: rgba(0, 0, 0, 1)">);
            NbaPlayerDO nbaPlayerDO2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
            nbaPlayerDO2.setCountryen(</span>"俄罗斯"<span style="color: rgba(0, 0, 0, 1)">);
            vo2.setJsonBody(JSON.toJSONString(nbaPlayerDO2));
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(vo2);
        }
    };
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag =<span style="color: rgba(0, 0, 0, 1)"> nbaService.batchAddDoc(NBA_INDEX_NAME, list);
    System.err.println(flag);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 异步批量添加文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> batchAsyncAddDoc() {
    List</span>&lt;EsDocumentVo&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;EsDocumentVo&gt;<span style="color: rgba(0, 0, 0, 1)">() {
        {
            EsDocumentVo vo1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> EsDocumentVo();
            vo1.setDocId(</span>"194"<span style="color: rgba(0, 0, 0, 1)">);
            NbaPlayerDO nbaPlayerDO1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
            nbaPlayerDO1.setCountryen(</span>"中国"<span style="color: rgba(0, 0, 0, 1)">);
            vo1.setJsonBody(JSON.toJSONString(nbaPlayerDO1));
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(vo1);
            EsDocumentVo vo2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> EsDocumentVo();
            vo1.setDocId(</span>"195"<span style="color: rgba(0, 0, 0, 1)">);
            NbaPlayerDO nbaPlayerDO2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NbaPlayerDO();
            nbaPlayerDO2.setCountryen(</span>"俄罗斯"<span style="color: rgba(0, 0, 0, 1)">);
            vo2.setJsonBody(JSON.toJSONString(nbaPlayerDO2));
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(vo2);
        }
    };
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag =<span style="color: rgba(0, 0, 0, 1)"> nbaService.batchAsyncAddDoc(NBA_INDEX_NAME, list);
    System.err.println(flag);
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) {

    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据文档id获取文档数据
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getDocumentByDocId() {
    Map</span>&lt;String, Object&gt; documentByDocId = nbaService.getDocumentByDocId(NBA_INDEX_NAME, "188"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(JSON.toJSON(documentByDocId));
}

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> updateDocumentByDocId() {
}

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> batchAsyncUpdateDocumentByDocId() {
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据文档id删除文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteDocumentByDocId() {
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag = nbaService.deleteDocumentByDocId(NBA_INDEX_NAME, "188"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(flag);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据索引名称删除所有文档
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> deleteAllDocumentByIndexName() {
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag =<span style="color: rgba(0, 0, 0, 1)"> nbaService.deleteAllDocumentByIndexName(NBA_INDEX_NAME);
    System.out.println(flag);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 导入所有球员数据到es
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> importAll() {
    List</span>&lt;NbaPlayerDO&gt; nbaPlayerDOList = nbaPlayerMapper.selectList(<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    List</span>&lt;EsDocumentVo&gt; documentVoList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">(nbaPlayerDOList.size());
    </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (NbaPlayerDO playerDO : nbaPlayerDOList) {
        EsDocumentVo vo </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> EsDocumentVo();
        vo.setDocId(playerDO.getId().toString());
        vo.setJsonBody(JSON.toJSONString(playerDO));
        documentVoList.add(vo);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> flag =<span style="color: rgba(0, 0, 0, 1)"> nbaService.batchAsyncAddDoc(NBA_INDEX_NAME, documentVoList);
    System.err.println(flag);
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) {

    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 根据名称查找球员
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> searchMatch() {
    List</span>&lt;NbaPlayerDO&gt; nbaPlayerDOList =<span style="color: rgba(0, 0, 0, 1)"> nbaService.searchMatch(
            NBA_INDEX_NAME,
            </span>"displaynameen"<span style="color: rgba(0, 0, 0, 1)">,
            </span>"james"<span style="color: rgba(0, 0, 0, 1)">,
            </span>1<span style="color: rgba(0, 0, 0, 1)">,
            </span>1000<span style="color: rgba(0, 0, 0, 1)">,
            </span>"displaynameen"<span style="color: rgba(0, 0, 0, 1)">
    );
    System.err.println(JSON.toJSON(nbaPlayerDOList));
}

}

源码下载#

链接: https://pan.baidu.com/s/1NpwqP3YCjLm5nLFKDZFzUQ?pwd=ubiv 提取码: ubiv