1.操作Elasticsearch
项目csmall-finish项目中
node文件夹下共享了ES文档,命令都在里面,可以测试
所有的代码都在"ES文档"中, 笔记略
下面我们要学习使用java代码来操作ES
2.SpringBoot 操作 Elasticsearch
2.1Spring Data简介
原生状态下,我们使用JDBC连接数据库,因为代码过于繁琐,所以改为使用Mybatis框架
在ES的原生状态下,我们java代码需要使用socket访问ES,但是也是过于繁琐,我们可以使用SpringData框架简化
Spring Data是Spring提供的一套连接各种第三方数据源的框架集
我们需要使用的是其中连接ES的Spring Data Elasticseatrch
官方网站:Spring Data
官网中列出了SpringData支持连接操作的数据源列表
下面我们就按照SpringDataElasticsearch的步骤对ES进行操作
2.2添加依赖和配置
就使用我们之前创建的search模块来操作ES
pom文件添加依赖
<?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>cn.tedu</groupId>
<artifactId>csmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>search</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>search</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring data elasticsearch 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 为了方便测试添加测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
application.properties添加配置
# 设置ES所在的ip地址端口号
spring.elasticsearch.rest.uris=http://localhost:9200
# 设置日志门槛,用于显示ES的运行信息
logging.level.cn.tedu.search=debug
# SpringDataElasticsearch底层有一个专门输出运行状态的类,也要设