SpringBoot 2.1.x版本整合 elasticsearch 7.4.0版本详细说明
一.首先介绍怎么使用docker安装elasticsearch7.4.0版本:
1.1拉取镜像:
docker pull elasticsearch:7.4.0
1.2创建容器:
docker run -id --name elasticsearch -d -p 9200:9200 -p 9300:9300 -v /usr/share/elasticsearch/plugins:/usr/share/elasticsearch/plugins -e "discovery.type=single-node" elasticsearch:7.4.0
1.3安装ik分词器:
第一步下载ik分词器压缩包,对应地址为:ik分词压缩包下载
这里的下载请注意!!! ik分词器的版本一定要跟elasticsearch的版本一致!!!
ik分词器的版本一定要跟elasticsearch的版本一致!!!
ik分词器的版本一定要跟elasticsearch的版本一致!!!
重要的事情说三边!!!
第二步将下载的ik分词器压缩包上传至虚拟机中,有的是服务器.我用的是自己的虚拟机,通过SecureCRT 工具将压缩包上传至虚拟机中.
SecureCRT 的安装 参考:SecureCRT的安装步骤
第三步进行解压:
#切换目录
cd /usr/share/elasticsearch/plugins
#新建目录
mkdir analysis-ik
cd analysis-ik
#查看es压缩包的绝对路径
pwd elasticsearch-analysis-ik-7.4.0.zip
#这是我的压缩包绝对路径 /home/itjava
#root根目录中拷贝文件
mv /home/itjava/elasticsearch-analysis-ik-7.4.0.zip /usr/share/elasticsearch/plugins/analysis-ik
#解压文件
cd /usr/share/elasticsearch/plugins/analysis-ik
unzip elasticsearch-analysis-ik-7.4.0.zip
#删除压缩包
rm -rf elasticsearch-analysis-ik-7.4.0.zip
解压完成后一定要删除压缩包,否则elasticsearch会闪退
第四步重启elasticsearch:
#查看所有的容器
docker ps -a
#启动容器
docker start 8b9d2962309c
1.4安装kibana:
#拉取镜像
docker pull kibana:7.4.0
#创建容器
docker run -d -p 5601:5601 --link elasticsearch -e "ELASTICSEARCH_URL=http://192.168.200.130:9200" kibana:7.4.0
1.5验证 http://192.168.200.140:5601
当出现以下界面代表安装成功
1.6创建库:
找到上图的控制台输入这句话:
PUT app_info_article
{
"mappings":{
"properties":{
"id":{
"type":"text"
},
"publishTime":{
"type":"date"
},
"layout":{
"type":"short"
},
"images":{
"type":"text"
},
"authorId": {
"type": "integer"
},
"title":{
"type":"text",
"analyzer":"ik_smart"
},
"content":{
"type":"text",
"analyzer":"ik_smart"
}
}
}
}
看到此图即为成功!
二.SpringBoot2.1.x整合 elasticsearch 7.4.0
首先引入坐标:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<!--elasticsearch-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.5</version>
</dependency>
</dependencies>
第二步添加application.yml配置文件:
server:
port: 8022
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 30000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: select 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=6000
application:
name: searchtest
#自定义elasticsearch连接配置
elasticsearch:
host: 192.168.200.140
port: 9200
第三步添加配置类:
/**
* @author zhangfc
* @date 2021/9/26 15:46
*/
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticConfiguretion {
private String host;
private Integer port;
@Bean
public RestHighLevelClient client() {
System.out.println(host);
System.out.println(port);
return new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port, "http")
)
);
}
}
配置准备就绪开始测试:
/**
* @author zhangfc
* @date 2021/9/26 15:54
*/
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class ElasticSearchTest {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
public void test01() throws IOException {
for (int i = 0; i < 10; i++) {
String id = UUID.randomUUID().toString().substring(0, 3);
//构建想要存储的对象
Map<String, Object> map = new HashMap<>();
map.put("id", id);
map.put("publishTime", new Date());
map.put("layout", RandomUtil.randomInt(1000));
map.put("images", RandomUtil.randomNumbers(10).toString());
map.put("authorId", RandomUtil.randomInt(1000));
map.put("content", RandomUtil.randomNumbers(10).toString());
//指定要存入数据的库
IndexRequest indexRequest
= new IndexRequest("app_info_article").id(id).source(map);
//创建索引 并保存至es库中
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
}
@Test
public void test02() throws IOException {
//构建搜索请求对象,需要指定索引库名称
SearchRequest searchRequest = new SearchRequest("app_info_article");
//条件构建器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//布尔查询
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//查询时间publishTime大于2021-09-21 15:00:00 的所有信息
RangeQueryBuilder rangeQueryBuilder = QueryBuilders
.rangeQuery("publishTime")
.gt(DateUtil.parse("2021-09-21 15:00:00").toJdkDate());
boolQueryBuilder.filter(rangeQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
//分页
searchSourceBuilder.from(0);
searchSourceBuilder.size(10);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
List<Map> articleList = new ArrayList<>();
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
Map map = JSONUtil.toBean(sourceAsString,Map.class);
articleList.add(map);
}
System.out.println(articleList);
}
}
上述测试只是按照时间筛选的方式进行查询,除此以外还有很多筛选方式查询 可自行查看api 更多api可查看:QueryBuilders的更多api用法