当前位置: 首页 > news >正文

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

核心区别对比

特性RestHighLevelClientRestClient
定位高级客户端(封装常用操作,推荐使用)底层HTTP客户端(更灵活,更复杂)
API风格面向对象(如 IndexRequestSearchRequest基于HTTP请求构建(如 RequestResponse
维护状态官方已停止维护(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);}

相关文章:

  • 分布式计算领域的前沿工具:Ray、Kubeflow与Spark的对比与协同
  • 蓝桥杯常用的APi
  • spatk-sql核心
  • ngx_conf_handler - worker_connections 1024
  • OpenResty与Nginx的功能对比分析
  • visual studio 如何在 release 模式下调试
  • 中美电力标准差异下电机运行的影响及应对策略
  • 接口和抽象的区别?日常使用场景
  • WINUI——Background颜色使用小结
  • 2 cline 提示词工程指南-记忆库
  • 基于PySide6与pycatia的CATIA智能倒角工具开发全解析
  • 4月15号
  • 欧冠002:阿斯顿维拉 vs 巴黎圣日耳曼,维拉强攻致防线大开
  • SP B\nRebuild Priorit> 如何用python去掉\n
  • 用python比较两个mp4是否实质相同
  • VLAN的知识
  • Enovia许可优化技巧
  • Dockerfile 文件常见命令及其作用
  • 微服务最佳实践:全链路可用性保障体系
  • 06- 服务网格实战:从 Istio 核心原理到微服务治理升级
  • 针对“二选一”,美团再次辟谣
  • 复旦大学史地学系在北碚
  • 全国登记在册民营企业超过5700万户,占企业总量92.3%
  • 解除近70家煤电厂有毒物质排放限制,特朗普能重振煤炭吗?
  • 城事|2小时40分42秒,天工夺冠!全球首个人形机器人半马开跑
  • 上海浦东:顶尖青年人才最高可获700万元资助及1亿元项目补贴