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底層會對我們的接口進行實現。