资料文档
链接: https://pan.baidu.com/s/1ALu0xN0SiipGSsaXy5ID4w 提取码: 999n 

第1集 NBA搜索实战之设计思路路

NBA搜索实战之设计思路路

  • 获取数据
    • 通过chrome浏览器器抓取nba球员数据
    • 将数据处理理后,导⼊到数据库
  • 项⽬目搭建
    • spring boot整合elastic search和mysql
  • 接口开发
    • 将数据库数据导⼊到elastic search
    • 通过姓名查找球员
    • 通过国家或者球队查询球员
    • 通过姓名字⺟查找球员

第2集 springboot整合elastic search和mysql

springboot整合elastic search和mysql

  • pom.xml
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.1.1</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source><!-- 源代码开发版本 -->
                    <target>1.8</target><!-- java编译版本 -->
                    <encoding>UTF8</encoding> <!-- 项目的编码 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

  • yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    druid:
      initial-size: 5 #连接池初始化大小
      min-idle: 10 #最小空闲连接数
      max-active: 20 #最大连接数
      web-stat-filter:
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" #不统计这些请求数据
      stat-view-servlet: #访问监控网页的登录用户名和密码
        login-username: druid
        login-password: druid

server:
  port: 8083

#mybatis:
#  mapper-locations:
#    - classpath:dao/*.xml
#    - classpath*:com/**/mapper/*.xml

logging:
  level:
    root: info
    com.xdclass.search: debug

elasticsearch:
  host: localhost
  port: 9200

  • EsConfig
/**
 * @author gtf
 * @date 2022/11/21 10:07
 */
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig {
    private String host;
    private Integer port;

    @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;
    }
}
  • model
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;
}
    @Select("select * from  nba_player")
    List<NBAPlayer> selectAll();

第3集 elastic search之java api的使⽤用

elastic search之java api的使⽤用

  • 添加一个文档
  @Autowired
    private RestHighLevelClient client;

    /**
     * 索引添加文档
     *
     * @param nbaPlayer 添加的对象
     * @param id        文档的id
     * @return
     */
    public boolean addPlayer(NBAPlayer nbaPlayer, String id) throws IOException {
        IndexRequest request = new IndexRequest("nba_laest").id(id).source(objectToMap(nbaPlayer));
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        return false;
    }
    /**
     * 对象转map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> objectToMap(Object object) {
        Map<String, Object> dataMap = new HashMap<>();
        Class<?> clazz = object.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                dataMap.put(field.getName(), field.get(object));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return dataMap;
    }
  • 根据文档id查询文档
 /**
     * 根据文档id查询文档
     *
     * @param id 文档的id
     * @return
     * @throws IOException
     */
    public Map<String, Object> getPlayer(String id) throws IOException {
        GetRequest getRequest = new GetRequest("nba_laest",id);
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        return response.getSource();
    }
  • 根据文档id修改文档
  /**
     * 根据文档id修改文档
     *
     * @param nbaPlayer 修改的对象
     * @param id        文档的id
     * @return
     * @throws IOException
     */
    public boolean updatePlayer(NBAPlayer nbaPlayer, String id) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("nba_laest", id).doc(objectToMap(nbaPlayer));
        UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
        return update.forcedRefresh();
    }
  • 根据文档id删除文档
    /**
     * 根据文档id删除文档
     *
     * @param id 文档的id
     * @return
     * @throws IOException
     */
    public boolean delPlayer(String id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("nba_laest", id);
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        return delete.forcedRefresh();
    }
  • 删除查询的球员
    /**
     * 删除查询的球员
     *
     * @return
     * @throws IOException
     */
    public boolean delALLPlayer() throws IOException {
        DeleteByQueryRequest request = new DeleteByQueryRequest("nba_laest");
        BulkByScrollResponse bulkByScrollResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
        return false;
    }

第4集 NBA搜索实战之导入球员数据

NBA搜索实战之导入球员数据

  • 将mysql数据导入到es
    public boolean impoartAll() {
        List<NBAPlayer> nbaPlayers = demoMapper.selectAll();
        nbaPlayers.forEach(obj -> {
            try {
                addPlayer(obj, obj.getId()+"");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return false;
    }

第5集 NBA搜索实战之通过姓名查找球员

NBA搜索实战之通过姓名查找球员

  • 使用Match查询
  /**
     * 根据指定key 查询数值
     *
     * @param key   列名
     * @param value 需要查询的值
     * @return
     */
    public List<NBAPlayer> searchMatch(String key, String value) throws IOException {
        //指定索引
        SearchRequest searchRequest = new SearchRequest("nba_laest");
        //指定查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery(key, value));
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(20);
        searchRequest.source(searchSourceBuilder);
        //开始查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        //获取结果
        SearchHit[] hits = response.getHits().getHits();
        ArrayList<NBAPlayer> nbaPlayers = new ArrayList<>();
        for (SearchHit hit : hits) {
            NBAPlayer nbaPlayer1 = JSONObject.parseObject(hit.getSourceAsString(), NBAPlayer.class);
            nbaPlayers.add(nbaPlayer1);
        }
        return nbaPlayers;
    }

第6集 NBA搜索实战之通过国家或球队查找球员

NBA搜索实战之通过国家或球队查找球员

  • term精确查询
/**
     * 指定 Term 查询 数值
     * @param key
     * @param value
     * @return
     * @throws IOException
     */
    public List<NBAPlayer> searchTerm(String key, String value) throws IOException {
        //指定索引
        SearchRequest searchRequest = new SearchRequest("nba_laest");
        //指定查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery(key, value));
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(20);
        searchRequest.source(searchSourceBuilder);
        //开始查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        //获取结果
        SearchHit[] hits = response.getHits().getHits();
        ArrayList<NBAPlayer> nbaPlayers = new ArrayList<>();
        for (SearchHit hit : hits) {
            NBAPlayer nbaPlayer1 = JSONObject.parseObject(hit.getSourceAsString(), NBAPlayer.class);
            nbaPlayers.add(nbaPlayer1);
        }
        return nbaPlayers;
    }

第7集 NBA搜索实战之通过字母查找球员

NBA搜索实战之通过字母查找球员

   /**
     * 根据指定key 查询数值
     * 前缀包含 value
     *
     * @param key   列名
     * @param value 需要查询的值
     * @return
     */
    public List<NBAPlayer> searchPrefix(String key, String value) throws IOException {
        //指定索引
        SearchRequest searchRequest = new SearchRequest("nba_laest");
        //指定查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.prefixQuery(key, value));
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(20);
        searchRequest.source(searchSourceBuilder);
        //开始查询
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        //获取结果
        SearchHit[] hits = response.getHits().getHits();
        ArrayList<NBAPlayer> nbaPlayers = new ArrayList<>();
        for (SearchHit hit : hits) {
            NBAPlayer nbaPlayer1 = JSONObject.parseObject(hit.getSourceAsString(), NBAPlayer.class);
            nbaPlayers.add(nbaPlayer1);
        }
        return nbaPlayers;
    }