ES通过API操作索引库
1. 导入restClient依赖
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.12.1</version></dependency>
2. 了解ES核心客户端API
核心区别对比
特性 RestHighLevelClient RestClient 定位 高级客户端(封装常用操作,推荐使用) 底层HTTP客户端(更灵活,更复杂) API风格 面向对象(如 IndexRequest
,SearchRequest
)基于HTTP请求构建(如 Request
,Response
)维护状态 官方已停止维护(ES 7.15+) 仍维护(但推荐迁移到新客户端) 依赖关系 基于 RestClient
实现是 RestHighLevelClient
的底层依赖适用场景 快速开发标准功能 需要自定义请求或访问未封装API
3. 将RestHighLevelClient注册成Bean
package com.example.demo.config;import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class ElasticSearchConfig {public static final String HOST = "127.0.0.1"; // es地址public static final int PORT = 9200; // es端口/*** 创建RestClient对象* @return*/@Beanpublic RestHighLevelClient restClient() {return new RestHighLevelClient(RestClient.builder(new HttpHost(HOST, PORT)));} }
4. 创建索引库
创建包和类用于保存索引结构:constants.HotelConstants
package com.example.demo.constants;public class HotelConstants {public static final String MAPPER_TEMPLATE_USER = "{\n" +" \"mappings\": {\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\", \n" +" \"doc_values\": true \n" +" },\n" +" \"username\": {\n" +" \"type\": \"text\", \n" +" \"analyzer\": \"ik_max_word\", \n" +" \"fields\": {\n" +" \"keyword\": {\n" +" \"type\": \"keyword\" \n" +" }\n" +" },\n" +" \"copy_to\": \"combined_search\" \n" +" },\n" +" \"password\": {\n" +" \"type\": \"keyword\", \n" +" \"index\": false \n" +" },\n" +" \"phone\": {\n" +" \"type\": \"keyword\", \n" +" \"ignore_above\": 20 \n" +" },\n" +" \"create_time\": {\n" +" \"type\": \"date\",\n" +" \"format\": \"yyyy-MM-dd HH:mm:ss||epoch_millis\" \n" +" },\n" +" \"update_time\": {\n" +" \"type\": \"date\",\n" +" \"format\": \"yyyy-MM-dd HH:mm:ss||epoch_millis\"\n" +" },\n" +" \"status\": {\n" +" \"type\": \"integer\", \n" +" \"null_value\": 1 \n" +" },\n" +" \"balance\": {\n" +" \"type\": \"integer\", \n" +" \"null_value\": 0 \n" +" },\n" +" \"combined_search\": { \n" +" \"type\": \"text\",\n" +" \"analyzer\": \"ik_smart\"\n" +" }\n" +" }\n" +" },\n" +" \"settings\": {\n" +" \"number_of_shards\": 3, \n" +" \"number_of_replicas\": 1, \n" +" \"analysis\": { \n" +" \"analyzer\": {\n" +" \"ik_smart\": { \n" +" \"type\": \"custom\",\n" +" \"tokenizer\": \"ik_max_word\"\n" +" }\n" +" }\n" +" }\n" +" }\n" +"}"; }
/*** 创建索引库*/@Testpublic void createIndex() throws IOException {// 创建请求,指定索引库名称CreateIndexRequest request = new CreateIndexRequest("user");//执行与数据库相对于的映射JSONCreateIndexRequest source = request.source(HotelConstants.MAPPER_TEMPLATE_USER, XContentType.JSON);// 发送请求//RequestOptions.DEFAULT:默认请求client.indices().create(source, RequestOptions.DEFAULT);}
执行前先确定是否删除user索引库,客户端查询一下如果为空就是删除了
# GET /user# DELETE /user
运行测试方法后再次查询
5. 判断索引库是否存在
/*** 判断索引库是否存在* @throws IOException*/@Testvoid testIndexExists() throws IOException {// 创建请求,指定索引库名称GetIndexRequest request = new GetIndexRequest("user");// 发送请求,判断索引库是否存在boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 打印结果,true表示存在,false表示不存在System.out.println(exists);}
6. 删除索引库
/*** 删除索引库* @throws IOException*/@Testvoid testDeleteIndex() throws IOException {// 创建请求,指定索引库名称DeleteIndexRequest request = new DeleteIndexRequest("user");// 发送请求client.indices().delete(request, RequestOptions.DEFAULT);}