1、基本语法已分享至百度网盘,此处不再描述了,可以下载后,使用postman工具导入此json文件进行学习基本语法
链接:https://pan.baidu.com/s/1lY7dB9Q9lewBF46z9ss5fw
提取码:pvq6
2、springboot使用api对接es,首先确定自己的springboot版本,找到对应的es版本去使用
3、确定好自己的版本后加入 es依赖包,比如我使用的boot版本为2.2.7版本,对应的es版本就应该是6.8.12,es客户端依赖的jar包最好也要保持同步,我的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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wxg</groupId>
<artifactId>es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!-- elasticsearch 的jar包 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.12</version>
</dependency>
<!-- elasticsearch 的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.8.12</version>
</dependency>
<!-- elasticsearch 依赖 2.x 的 log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
4、在springboot写一个main方法,测试是否可以正常使用,本次示例直接创建一个新的索引
package com.wxg.es.test;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
public class createIndex {
public static void main(String[] args) throws IOException {
//创建es客户端对象
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//创建索引请求
CreateIndexRequest user = new CreateIndexRequest("user");
//创建索引,得到Response返回
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(user, RequestOptions.DEFAULT);
//获取执行结果 true为成功,false为失败
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引创建:"+acknowledged);
//关闭客户端对象
restHighLevelClient.close();
}
}
5、在postman中找到“查询所有索引”进行查询,可以看到已经新建成功了
6、以上是新建部分的代码,删除(DeleteIndexRequest)、查询(GetIndexRequest),基本都是同理
7、创建索引后,我们来向索引文档中添加数据,新建一个IndexInsert.class文件
package com.wxg.es.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class IndexInsert {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
// 新增文档 - 请求对象
IndexRequest request = new IndexRequest();
// 设置索引及唯一性标识
request.type("String");
request.index("user").id("1001");
// 创建数据对象
User user = new User();
user.setName("zhangsan");
user.setAge(30);
user.setSex("男");
ObjectMapper objectMapper = new ObjectMapper();
String productJson = objectMapper.writeValueAsString(user);
// 添加文档数据,数据格式为 JSON 格式
request.source(productJson,XContentType.JSON);
// 客户端发送请求,获取响应对象
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println("_index:" + response.getIndex());
client.close();
}
}
8、值得注意的是es7.x后默认都是_doc类型的type,但是我们现在用得是6.8,所以,我们在写入文档时候,必须指定一下type类型,不然会报以下错误
9、解决办法就是在上述代码中,添加一个type即可 ,如下,然后再次执行即可插入成功
10、修改文档代码如下,如果是6版本的记得设置type,如9步骤所示