ElasticSearch的学习
一、ES的基础学习
二、ES与SpringBoot的集成
SpringBoot默认支持两种技术来和ES交互;
1、Jest(但是默认是不生效的) 需要导入jest的工具包:io.searchbox.client.JestClient;
2、SpringData ElasticSearch:
1)、Client节点信息:clusterNodes:clusterName;
2)、ElasticSearchTemplate操作es;
3)、编写一个ElasticSearchRepository的子接口来操作es;
3、开始实践操作:
一、新建一个springboot项目;
二、添加jest、elasticsearch的依赖;
<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.elasticsearch</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--Jest-->
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、添加配置文件:
application.properties
这里把两种连接的方式都配置好,后面好实践。
我们要明白的是:jest连接是基于http通信的,springdata是基于tcp通信的;
# 使用http通信
spring.elasticsearch.jest.uris=http://47.97.192.241:9200
# 使用tcp通信
#springdata 配置es
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=47.97.192.241:9300
如果你们的es连接失败,可能是版本不适配问题:安装对应版本即可。
Spring Data Elasticsearch | Elasticsearch |
---|---|
3.2.x | 6.7.2 |
3.1.x | 6.2.2 |
3.0.x | 5.5.0 |
2.1.x | 2.4.0 |
2.0.x | 2.2.0 |
1.3.x | 1.5.2 |
四、Jest连接测试:
1、添加索引到es中
@SpringBootTest
class DemoApplicationTests {
@Autowired
JestClient jestClient;
@Test
void contextLoads() {
System.out.println("hello world");
}
@Test
void add() throws IOException {
User user=new User();
user.setId(1);
user.setUsername("欧光继");
user.setAddress("重庆市");
user.setPasswd("123456");
user.setYoubian("402460");
user.setPhone("17623824306");
//构建一个索引
Index index=new Index.Builder(user).index("coreqi").type("user").build();
//执行
DocumentResult documentResult=jestClient.execute(index);
System.out.println(documentResult.getJsonString());
}
}
2、查看结果:
得到以下结果,添加成功。
五、SpringData连接ElasticSearch:
测试类:
@Test
void test(){
User user=new User();
user.setId(1);
user.setUsername("欧光继");
user.setAddress("重庆市");
user.setPasswd("123456");
user.setYoubian("402460");
user.setPhone("17623824306");
userRepository.save(user);
}
检验是否添加成功:
总结:
在日常使用ES中,我们常常喜欢使用springdata来对es进行访问,因为repository中都带有了常用的一些操作,我们只需要调取接口函数即可:
并且我们也可以在接口中自定义函数方法,来规定自己所需的操作。并且只需要按照ES官方文档来写方法名就可以了,不用写方法实现,repository底层会对我们的接口进行实现。