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

二十三、RestClient操作索引库

目录

例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在

1、编写mapping映射

2、初始化JavaRestClient

(1)导入elasticsearch的依赖

(2)修改elasticsearch的版本

(3)初始化RestHighLevelClient

3、创建索引库

(1)编写代码

(2)创建常量类,将mapping映射作为常量导入

(3)运行测试

4、删除索引库

5、查询索引库是否存在


例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在

1、编写mapping映射

PUT /hotel
{"mappings": {"properties": {"id": {"type": "keyword"},"name":{"type": "text","analyzer": "ik_max_word","copy_to": "all"},"address":{"type": "keyword","index": false},"price":{"type": "integer"},"score":{"type": "integer"},"brand":{"type": "keyword","copy_to": "all"},"city":{"type": "keyword"},"starName":{"type": "keyword"},"business":{"type": "keyword","copy_to": "all"},"location":{"type": "geo_point"},"pic":{"type": "keyword","index": false},"all":{"type": "text","analyzer": "ik_max_word"}}}
}

2、初始化JavaRestClient

(1)导入elasticsearch的依赖
        <dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
(2)修改elasticsearch的版本
    <properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version></properties>
(3)初始化RestHighLevelClient

新建测试类并编写代码

public class HotelIndexTest {private RestHighLevelClient client;@Testvoid testInit(){System.out.println(client);}@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.248.152:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}
}

3、创建索引库

(1)编写代码

public class HotelIndexTest {private RestHighLevelClient client;@Testvoid testInit(){System.out.println(client);}@BeforeEachvoid setUp(){this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.248.152:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}@Testvoid createHotelIndex() throws IOException {
//        创建request对象CreateIndexRequest request = new CreateIndexRequest("hotel");
//        准备请求参数request.source(MAPPING, XContentType.JSON);
//        发送请求client.indices().create(request, RequestOptions.DEFAULT);}}
(2)创建常量类,将mapping映射作为常量导入


public class HotelConstans {public static final String MAPPING ="{\n" +"  \"mappings\": {\n" +"    \"properties\": {\n" +"      \"id\": {\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"name\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"address\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"price\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"score\":{\n" +"        \"type\": \"integer\"\n" +"      },\n" +"      \"brand\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"        \n" +"      },\n" +"      \"city\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"starName\":{\n" +"        \"type\": \"keyword\"\n" +"      },\n" +"      \"business\":{\n" +"        \"type\": \"keyword\",\n" +"        \"copy_to\": \"all\"\n" +"      },\n" +"      \"location\":{\n" +"        \"type\": \"geo_point\"\n" +"      },\n" +"      \"pic\":{\n" +"        \"type\": \"keyword\",\n" +"        \"index\": false\n" +"      },\n" +"      \"all\":{\n" +"        \"type\": \"text\",\n" +"        \"analyzer\": \"ik_max_word\"\n" +"      }\n" +"    }\n" +"  }\n" +"}";
}
(3)运行测试

4、删除索引库

    @Testvoid deleteHotelIndex() throws IOException {
//        创建request对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");
//        发送请求client.indices().delete(request, RequestOptions.DEFAULT);}

5、查询索引库是否存在

    @Testvoid existsHotelIndex() throws IOException {
//        创建request对象GetIndexRequest request = new GetIndexRequest("hotel");
//        发送请求boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "已经存在":"不存在");}

相关文章:

  • EPT-Net:用于3D医学图像分割的边缘感知转换器
  • gitlab图形化界面使用
  • Verilog基础:时序调度中的竞争(一)
  • ElasticSearch之cat aliases API
  • Redis中文结果查看方式
  • 【Python 千题 —— 基础篇】删除列表值
  • Nginx模块开发之http过滤器filter
  • MySQL面试,MySQL事务,MySQL锁,MySQL集群,主从,MySQL分区,分表,InnoDB
  • 蓝桥杯每日一题2023.11.23
  • 【算法专题】滑动窗口—无重复字符的最长子串
  • Django项目window环境部署
  • Python之Pygame游戏编程详解
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(二十一)
  • Missing file libarclite_iphoneos.a 问题解决方案
  • Halcon Solution Guide I basics(4): Blob Analysis(连通性解析)
  • [case10]使用RSQL实现端到端的动态查询
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • co.js - 让异步代码同步化
  • CSS 三角实现
  • HomeBrew常规使用教程
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Octave 入门
  • php面试题 汇集2
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 二维平面内的碰撞检测【一】
  • 分类模型——Logistics Regression
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 关于for循环的简单归纳
  • 猴子数据域名防封接口降低小说被封的风险
  • 如何用vue打造一个移动端音乐播放器
  • 一道闭包题引发的思考
  • 数据可视化之下发图实践
  • ​Linux·i2c驱动架构​
  • ​linux启动进程的方式
  • # windows 安装 mysql 显示 no packages found 解决方法
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (C)一些题4
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (附源码)springboot金融新闻信息服务系统 毕业设计651450
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (九十四)函数和二维数组
  • (四)opengl函数加载和错误处理
  • (推荐)叮当——中文语音对话机器人
  • (转载)深入super,看Python如何解决钻石继承难题
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .env.development、.env.production、.env.staging
  • .naturalWidth 和naturalHeight属性,
  • .net core + vue 搭建前后端分离的框架
  • .NET Micro Framework初体验
  • .NET使用存储过程实现对数据库的增删改查
  • .NET业务框架的构建
  • .NET中两种OCR方式对比
  • ::什么意思
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?