Elasticsearch的java api使用
前提搭建了集群名字为my-elasticsearch的集群环境,并且正常访问。
ES简单的JavaAPI操作
注意,如果要用api操作ES,对应jar包的版本和ES版本要对应上,否则会报错,我用的ES版本是7.9.3
Spring Data项目对Elasticsearch做了支持,其目的就是简化对Elasticsearch的操作。
地址:https://spring.io/projects/spring-data-elasticsearch
导入依赖
这里采用SpringBoot整合的方式进行。
编写application.properties
spring.application.name = itcast-elasticsearch
spring.data.elasticsearch.cluster-name=es-itcast-cluster
spring.data.elasticsearch.clusternodes=
127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302
这里要注意,使用的端口是9300,而并非9200,原因是9200是RESTful端口,9300是API端口。
编写测试用例
package cn.itcast.es;
import cn.itcast.es.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSpringBootES {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void testSave(){
User user = new User();
user.setId(1001L);
user.setAge(20);
user.setName("张三");
user.setHobby("足球、篮球、听音乐");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(user).build();
String index = this.elasticsearchTemplate.index(indexQuery);
System.out.println(index);
}
}
运行后出现第一个问题
Cause By:java.lang.ClassNotFoundException:org.elasticsearch.action.Action
然后查了好多资料也没弄清,主要是正常启动整个项目不出错,只在Junit出现问题,所以检查下Maven的问题。
解决办法:
我的elasticsearch是7.9.3
很简单…因为使用的elasticsearch的版本太高了,所以把springboot的parent更新到2.3.6就好了如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
</parent>
添加后出现新的问题:
pom文件出现unkown error
首行错误 Unknow Error解决方法
添加:<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
继续运行,还是出现错误junit 错误.
因为sprint boot 2.3.1要使用更高版本的junit5
导入junit5包
<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>
修改测试代码 @ExtendWith(SpringExtension.class)
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
package cn.itcast.es;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import cn.itcast.es.pojo.User;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TestSpringBootES {
@Autowired
private ElasticsearchRestTemplate elasticsearchTemplate;
@Test
public void testSave() {
User user = new User();
user.setId(1001L);
user.setAge(20);
user.setName("张三");
user.setHobby("足球、篮球、听音乐");
IndexQuery indexQuery = new IndexQueryBuilder().withObject(user).build();
String index = this.elasticsearchTemplate.index(indexQuery, IndexCoordinates.of("itcast"));
System.out.println(index);
}
}
继续运行,还是出现错误
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
修改pom导入org.slf4j包
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
最后才运行成功!
完整的pom.xml
<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.3.6.RELEASE</version>
</parent>
<groupId>itcast-elasticsearch</groupId>
<artifactId>itcast-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>