文末附上项目的下载地址
前面将文档的RestAP单元测试代码上传,这里补上索引的RestAPI单元测试,同时补上pom.xml文件,pom文件在我这里是子项目的,参考即可
API同样采用ES8的链式编程代码风格
pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.toto</groupId>
<artifactId>totostudy</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-elasticsearch</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<elasticsearch.version>8.12.2</elasticsearch.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- SpringBoot 核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- SpringBoot Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.12.2</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
<exclusion>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.12.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
索引单元测试类
package com.toto.es.hotel;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
/**
* @Description: HotelIndexTest
* @Package: com.toto.es
* @Author gufanbiao
* @CreateTime 2024-05-03 15:06
*/
@SpringBootTest
@SuppressWarnings("all")
public class HotelIndexTest {
private RestClient restClient;
private ElasticsearchClient client;
private ElasticsearchTransport transport;
/**
* 测试初始化
*/
@Test
void testInit(){
System.out.println(client);
}
/**
* 查询所有索引
* @throws IOException
*/
@Test
void testGetAllIndex() throws IOException {
GetIndexResponse all = client.indices().get(query -> query.index("*"));
GetIndexResponse all2 = client.indices().get(query -> query.index("_all"));
System.err.println(all.toString());
System.err.println(all2.toString());
}
/**
* 查询某个索引
* @throws IOException
*/
@Test
void testGetOneIndex() throws IOException {
GetIndexResponse products = client.indices().get(query -> query.index("hotel"));
System.err.println(products.toString());
}
/**
* 创建索引
* @throws IOException
*/
@Test
void testCreateHotelIndex() throws IOException {
//查询某个索引是否存在
boolean exists = client.indices().exists(query -> query.index("student2")).value();
System.out.println(exists);
if (exists) {
System.err.println("索引已存在");
} else {
CreateIndexResponse products = client.indices().create(builder -> builder.index("student2"));
System.err.println(products.acknowledged());
}
}
/**
* 删除指定索引
*/
@Test
void testDeleteIndex() throws IOException {
boolean exists = client.indices().exists(query -> query.index("student2")).value();
if (exists) {
DeleteIndexResponse response = client.indices().delete(query -> query.index("student2"));
System.err.println(response.acknowledged());
} else {
System.err.println("索引不存在");
}
}
/**
* 查询索引的映射
*/
@Test
void testGetMapping() throws IOException {
GetIndexResponse response = client.indices().get(query -> query.index("hotel"));
System.err.println(response.toString());
}
/**
* 创建索引指定映射
* numberOfReplicas(“1”):设置副本
* numberOfShards(“1”):设置分片
*/
@Test
void testCreateIndexWithMapping() throws IOException {
CreateIndexResponse response = client.indices().create(builder ->
builder.settings(indexSetting -> indexSetting.numberOfReplicas("1").numberOfShards("1"))
.mappings(builder2 -> builder2
.properties("id", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty))
.properties("name", propertyBuilder ->
propertyBuilder.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word").copyTo("all")))
.properties("address", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty.index(false)))
.properties("price", propertyBuilder ->
propertyBuilder.integer(integerProperty -> integerProperty))
.properties("score", propertyBuilder ->
propertyBuilder.integer(integerProperty -> integerProperty))
.properties("brand", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty.copyTo("all")))
.properties("city", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty))
.properties("startName", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty))
.properties("business", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty.copyTo("all")))
.properties("location", propertyBuilder ->
propertyBuilder.geoPoint(geoProperty -> geoProperty))
.properties("pic", propertyBuilder ->
propertyBuilder.keyword(keywordProperty -> keywordProperty.index(false)))
.properties("all", propertyBuilder ->
propertyBuilder.text(textProperty -> textProperty.analyzer("ik_max_word").searchAnalyzer("ik_max_word")))
).index("hotel")
);
System.err.println(response.acknowledged());
}
@BeforeEach
void setup() {
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "elastic"));
restClient = RestClient.builder(HttpHost.create("http://127.0.0.1:9200"))
.setHttpClientConfigCallback(hc -> hc.setDefaultCredentialsProvider(credsProv))
.build();
transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
client = new ElasticsearchClient(transport);
}
@AfterEach
void tearDown() throws Exception{
client.shutdown();
transport.close();
restClient.close();
}
}