跨境互联网 跨境互联网
首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)

Revin

首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)
  • MySQL优化

  • MySQL海量数据存储与优化

  • 索引优化注意
  • MySQL平滑扩容实战
  • TiDB

  • 图和Neo4j

  • MongoDB

  • 缓存

  • 文件存储

  • Elasticsearch

    • 1 Elasticsearch基础
    • 2 Elasticsearch入门使用
    • 3 Elasticsearch高级应用
    • 4 Elasticsearch高可用分布式集群
    • 5 Elasticsearch数据模型构建
    • 6 Elasticsearch搜索实战
      • 1 案例需求
      • 2 代码实现
        • 2.1 项目准备
        • 2.2 代码编写
        • 1) application.yml 文件
        • 2) Model
        • 2) ES配置类
        • 3) 连接mysql的工具类DBHelper
        • 4) Service 接口 和 实现类
        • 5) 控制器
        • 6) 启动引导
        • 7) 页面index
    • 7 Elasticsearch深度应用及原理
    • 8 Elasticsearch操作总结
  • 数据库与缓存
  • Elasticsearch
Revin
2023-07-15
目录

6 Elasticsearch搜索实战

# 1 案例需求

Elasticsearch7x_Page117_001

MySQL中的数据批量导入到ES中, 然后进行搜索职位信息 展示出职位的信息

# 2 代码实现

# 2.1 项目准备

1)数据库准备 -- 执行 position.sql 脚本

2)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.1.0.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.lagou</groupId>
   <artifactId>lagou-es-project</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>lagou-es-project</name>
   <description>Demo project for Spring Boot</description>
   <properties>
       <elasticsearch.version>7.3.0</elasticsearch.version>
   </properties>
   <dependencies>
       <dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>elasticsearch-rest-high-level-client</artifactId>
           <version>${elasticsearch.version}</version>
       </dependency>
       <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
-->
       <dependency>
           <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
           <version>${elasticsearch.version}</version>
       </dependency>
       <dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>elasticsearch-rest-high-level-client</artifactId>
           <version>7.3.0</version>
           <exclusions>
               <exclusion>
                   <groupId>org.elasticsearch</groupId>
                   <artifactId>elasticsearch</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
           <version>7.3.0</version>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <scope>runtime</scope>
           <optional>true</optional>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-configuration-processor</artifactId>
           <optional>true</optional>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <optional>true</optional>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
           <exclusions>
               <exclusion>
                   <groupId>org.junit.vintage</groupId>
                   <artifactId>junit-vintage-engine</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <!-- HttpClient -->
       <dependency>
           <groupId>org.apache.httpcomponents</groupId>
           <artifactId>httpclient</artifactId>
           <version>4.5.3</version>
       </dependency>
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.58</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>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>
       <!--devtools热部署-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-devtools</artifactId>
           <optional>true</optional>
           <scope>true</scope>
       </dependency>
   </dependencies>
   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

# 2.2 代码编写

# 1) application.yml 文件

spring:
  devtools:
    restart:
      enabled: true  #设置开启热部署
      additional-paths: src/main/java #重启目录
      exclude: WEB-INF/**
    freemarker:
      cache: false    #页面不加载缓存,修改即时生效
  elasticsearch:
    rest:
      uris: 192.168.211.136:9200,192.168.211.136:9201,192.168.211.136:9202
server:
  port: 8083
logging:
  level:
    root: info
    com.xdclass.search: debug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 2) Model

package com.lagou.es.model;
import com.alibaba.fastjson.annotation.JSONType;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Position {
   //主键
   private String id;
   //公司名称
   private String companyName;
   //职位名称
   private String positionName;
   //职位诱惑
   private String  positionAdvantage;
   //薪资
   private String salary;
   //薪资下限
   private int salaryMin;
   //薪资上限
   private int salaryMax;
   //学历
   private String education;
   //工作年限
   private String workYear;
   //发布时间
   private String publishTime;
   //工作城市
   private String city;
   //工作地点
   private String workAddress;
   //发布时间
   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   private Date createTime;
   //工作模式
   private String jobNature;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 2) ES配置类

package com.lagou.es.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EsConfig {
   @Value("${spring.elasticsearch.rest.uris}")
   private  String  hostlist;
   @Bean
   public RestHighLevelClient client() {
       //解析hostlist配置信息
       String[] split = hostlist.split(",");
       //创建HttpHost数组,其中存放es主机和端口的配置信息
       HttpHost[] httpHostArray = new HttpHost[split.length];
       for(int i=0;i<split.length;i++){
           String item = split[i];
           System.out.println(item);
           httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
     
}
       //创建RestHighLevelClient客户端
       return new RestHighLevelClient(RestClient.builder(httpHostArray));
 
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 3) 连接mysql的工具类DBHelper

package com.lagou.es.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelper {
   public static final String url = "jdbc:mysql://192.168.211.136:3306/lagou_position?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";


   public static final String name = "com.mysql.cj.jdbc.Driver";
   public static final String user = "root";
   public static final String password = "123456";
   public static Connection conn = null;
   public static Connection getConn() {
       try {
           Class.forName(name);
           conn = DriverManager.getConnection(url, user, password);//获取连接
     
} catch (Exception e) {
           e.printStackTrace();
     
}
       return conn;
 
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 4) Service 接口 和 实现类

public interface PositionService {
   /**
    * 分页查询
    * @param keyword
    * @param pageNo
    * @param pageSize
    * @return
    */
   public List<Map<String,Object>> searchPos(String keyword , int pageNo , int pageSize) throws IOException;
   /**
    * 导入数据
    */
   void importAll() throws IOException;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

PositionServiceImpl

package com.lagou.es.service.impl;
import com.lagou.es.config.EsConfig;
import com.lagou.es.service.PositionService;
import com.lagou.es.util.DBHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;


@Service
public class PositionServiceImpl implements PositionService {
   private static final Logger logger = LogManager.getLogger(PositionServiceImpl.class);


   @Autowired
   private RestHighLevelClient client;
   private static final String POSITIOIN_INDEX = "position";
   //查找职位
   public List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {
       if (pageNo <= 1) {
           pageNo = 1;
     
}
       //getPosition(keyword);
       //条件搜索
       SearchRequest searchRequest = new SearchRequest(POSITIOIN_INDEX);
       SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
       //分页 index = (当前页-1)*一页显示条数
       searchSourceBuilder.from((pageNo - 1) * pageSize);
       searchSourceBuilder.size(pageSize);
       //精准匹配
       //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("positionName",keyword);
       //searchSourceBuilder.query(termQueryBuilder);
       QueryBuilder builder = QueryBuilders.matchQuery("positionName", keyword);
       searchSourceBuilder.query(builder);
       
       searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
       //执行搜索
       searchRequest.source(searchSourceBuilder);
       SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
       ArrayList<Map<String, Object>> list = new ArrayList<>();
       SearchHit[] hits = searchResponse.getHits().getHits();
       System.out.println(hits.length);
       for (SearchHit hit : hits) {
           list.add(hit.getSourceAsMap());
     
        }
       return list;
  }
  
   @Override
   public void importAll() throws IOException {
       writeMysqlDataToES(POSITIOIN_INDEX);
 
    }
    
   /** 讲数据批量写入ES中 */
   private  void writeMysqlDataToES(String tableName) {
       BulkProcessor bulkProcessor = getBulkProcessor(client);
       Connection conn = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       try {
           conn = DBHelper.getConn();
           logger.info("Start handle data :" + tableName);
           String sql = "SELECT * from " + tableName;
           ps = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
           // 根据自己需要 设置
           ps.setFetchSize(20);
           rs = ps.executeQuery();
           ResultSetMetaData colData = rs.getMetaData();
           ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
           // bulkProcessor 添加的数据支持的方式并不多,查看其api发现其支持map键值对的方式,故笔者在此将查出来的数据转换成hashMap方式
           HashMap<String, String> map = null;
           int count = 0;
           String c = null;
           String v = null;
           while (rs.next()) {
               count++;
               map = new HashMap<String, String>(128);
               for (int i = 1; i <= colData.getColumnCount(); i++) {
                   c = colData.getColumnName(i);
                   v = rs.getString(c);
                   map.put(c, v);
             
            }
               dataList.add(map);
               // 每1万条写一次,不足的批次的最后再一并提交
               if (count % 10000 == 0) {
                   logger.info("Mysql handle data number : " + count);
                   // 将数据添加到 bulkProcessor 中
                   for (HashMap<String, String> hashMap2 : dataList) {
                       bulkProcessor.add(
                           new IndexRequest(POSITIOIN_INDEX).source(hashMap2));
                 
                }
                   // 每提交一次便将map与list清空
                   map.clear();
                   dataList.clear();
             
         
            }
        }
           // 处理未提交的数据
           for (HashMap<String, String> hashMap2 : dataList) {
               bulkProcessor.add(
                       new IndexRequest(POSITIOIN_INDEX).source(hashMap2));
               System.out.println(hashMap2);
         
}
           logger.info("-------------------------- Finally insert number total: " + count);
           // 将数据刷新到es, 注意这一步执行后并不会立即生效,取决于bulkProcessor设置的刷新时间
           bulkProcessor.flush();
     
} catch (Exception e) {
           logger.error(e.getMessage());
     
} finally {
           try {
               rs.close();
               ps.close();
               conn.close();
               boolean terminatedFlag = bulkProcessor.awaitClose(150L,
                   TimeUnit.SECONDS);
               logger.info(terminatedFlag);
         
} catch (Exception e) {
               logger.error(e.getMessage());
         
     
 
}
}
}
   private BulkProcessor getBulkProcessor(RestHighLevelClient client) {
       BulkProcessor bulkProcessor = null;
       try {
           BulkProcessor.Listener listener = new BulkProcessor.Listener() {
               @Override
               public void beforeBulk(long executionId, BulkRequest request) {
                   logger.info("Try to insert data number : "
                               + request.numberOfActions());
             
}
               @Override
               public void afterBulk(long executionId, BulkRequest request,
                                     BulkResponse response) {
                   logger.info("************** Success insert data number : "
                            + request.numberOfActions() + " , id: " + executionId);
             
}
               @Override
               public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                   logger.error("Bulk is unsuccess : " + failure + ", executionId: " + executionId);
             
         
}
};
           BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer = (request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
           
           BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer, listener);
           builder.setBulkActions(5000);
           builder.setBulkSize(new ByteSizeValue(100L, ByteSizeUnit.MB));
           builder.setConcurrentRequests(10);
           builder.setFlushInterval(TimeValue.timeValueSeconds(100L));
         
 builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3));
           // 注意点:让参数设置生效
           bulkProcessor = builder.build();
     
} catch (Exception e) {
           e.printStackTrace();
           try {
               bulkProcessor.awaitClose(100L, TimeUnit.SECONDS);
         
} catch (Exception e1) {
               logger.error(e1.getMessage());
         
     
}
}
       return bulkProcessor;
 
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

BulkProcessor 官网介绍

https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.3/java-docs-bulk-processor.html

# 5) 控制器

package com.lagou.es.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lagou.es.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Controller
public class PositionController {


   @Autowired
   private PositionService service;
   //测试范文页面
   @GetMapping({"/","index"})
   public String indexPage(){
       return "index";
 
   }
   
   @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
   @ResponseBody
   public List<Map<String, Object>> searchPosition(@PathVariable("keyword") String keyword ,      @PathVariable("pageNo") int pageNo,                                   @PathVariable("pageSize") int pageSize) throws IOException {
       List<Map<String,Object>> list = service.searchPos(keyword,pageNo,pageSize);
       System.out.println(list);
       return list;
 
  }
  
   @RequestMapping("/importAll")
   @ResponseBody
   public String importAll(){
       try {
           service.importAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
          return "success";
    }
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 6) 启动引导

package com.lagou.es;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SearchApplication {
   public static void main(String[] args) {
       SpringApplication.run(SearchApplication.class, args);
  }
}
1
2
3
4
5
6
7
8
9
10
11

# 7) 页面index

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <link rel="stylesheet" href="layout_8a42639.css">
   <link rel="stylesheet"  href="main.html_aio_2_29de31f.css">
   <link rel="stylesheet"  href="main.html_aio_16da111.css">
   <link rel="stylesheet"  href="widgets_03f0f0e.css">
   <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
   <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>


<body>
<div id="top_bannerC">
   <a rel="nofollow" href="https://zhuanti.lagou.com/qiuzhaozhc.html"
      style="background:url(https://www.lgstatic.com/i/image/M00/3C/48/CgqCHl8nkiOAOihGAAC5a9EQ2rU358.PNG) center center no-repeat"
    target="_blank" data-lg-tj-id="bd00" data-lg-tj-no="idnull" data-lg-tj-cid="11127" class=""></a>
</div>
<div id="lg_header" >
   <!-- 页面主体START -->
   <div id="content-container">
       <div class="search-wrapper" style="height: 180px;">
           <div id="searchBar" class="search-bar" style="padding-top: 30px;">
               <div class="tab-wrapper" style="display: block;">
                   <a id="tab_pos" class="active" rel="nofollow"
href="javascript:;">职位 ( <span>500+</span> ) </a>
                   <a id="tab_comp" class="disabled" rel="nofollow"
href="javascript:;">公司 ( <span>0</span> ) </a>
               </div>
               <div class="input-wrapper" data-lg-tj-track-code="search_search" data-lg-tj-track-type="1">
                   <div class="keyword-wrapper">
                       <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span><input
                           type="text" id="keyword" v-model="keyword"
autocomplete="off" maxlength="64" placeholder="搜索职位、公司或地点"
                           value="java" class="ui-autocomplete-input"
 @keyup.enter="searchPosition">
                   </div>
                   <input type="button" id="submit"
@click.prevent="searchPosition" value="搜索">
               </div>
           </div>
       </div>
       <!-- 搜索输入框模块 -->
       <div id="main_container">
           <!-- 左侧 -->
           <div class="content_left">
               <div class="s_position_list " id="s_position_list">
                   <ul class="item_con_list" style="display: block;">
                       <li class="con_list_item default_list"  v-for="element in results">
                           <span class="top_icon direct_recruitment"></span>
                           <div class="list_item_top">
                               <div class="position">
                                   <div class="p_top">
                                       <h3 style="max-width: 180px;">
{{element.positionName}}</h3>
                                       <span class="add">[<em>
{{element.workAddress}}</em>]</span>
                                       <span class="format-time">
{{element.createTime}} 发布</span>
                                   </div>
                                   <div class="p_bot">
                                       <div class="li_b_l">
                                           <span class="money">
{{element.salary}}</span>
                                         
经验{{element.workYear}}年 / {{element.education}}
                                       </div>
                                   </div>
                               </div>
                               <div class="company">
                                   <div class="company_name">
                                       <a href="#" target="_blank">
{{element.companyName}}</a>
                                       <i class="company_mark"><span>该企业已经上传营业执照并通过资质验证审核</span></i>
                                   </div>
                                   <div class="industry">
                                     福利 【{{element.positionAdvantage}}】
                                   </div>
                               </div>
                               <div class="com_logo">
                                   <img src="//www.lgstatic.com/thumbnail_120x120/i/image2/M01/79/70/CgotOV1aS4qAWK6WAAAM4NTpXws809.png" alt="拉勾渠道" width="60" height="60">
                               </div>
                           </div>
                       </li>
                   </ul>
               </div>
           </div>
       </div>
   </div>
</div>
</div>
</body>
<script>
   new Vue({
       el: "#lg_header",
       data: {
           keyword : "", //搜索关键字
           results
     
: []   //查询结果
},
       methods : {
           searchPosition(){
              var keyword =  this.keyword;
              console.log(keyword);
             
axios.get('http://localhost:8083/search/'+keyword+'/1/10').then(response=>{
                  console.log(response.data);
                  this.results = response.data;
              })
         
     
 
}
})
</script>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
上次更新: 2025/04/03, 11:07:08
5 Elasticsearch数据模型构建
7 Elasticsearch深度应用及原理

← 5 Elasticsearch数据模型构建 7 Elasticsearch深度应用及原理→

最近更新
01
tailwindcss
03-26
02
PaddleSpeech
02-18
03
whisper
02-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 跨境互联网 | 豫ICP备14016603号-5 | 豫公网安备41090002410995号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式