ES的RestClient相关操作

2024-04-01 03:20
文章标签 es 操作 相关 restclient

本文主要是介绍ES的RestClient相关操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ES的RestClient相关操作

Elasticsearch使用Java操作。

本文仅介绍CURD索引库和文档!!!

Elasticsearch基础:https://blog.csdn.net/weixin_46533577/article/details/137207222

Elasticsearch Clients官网:https://www.elastic.co/guide/en/elasticsearch/client/index.html

文档相关:https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/getting-started-java.html

Java环境

规定ES版本。

<properties><java.version>1.8</java.version><!-- elasticsearch版本控制全局 --><elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

ES的版本为7.12.1。

<!--FastJson-->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.47</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId>
</dependency>
<!-- elasticsearch -->
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

初始化RestClient

在连接开始前初始化连接对象,IP地址。

在后面关闭资源。

RestClient.builder中可以添加多个连接,方便集群环境。

import cn.itcast.hotel.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import java.io.IOException;public class HotelIndexTest {private RestHighLevelClient client;@BeforeEachvoid setup() throws Exception {this.client = new RestHighLevelClient(RestClient.builder((HttpHost.create("http://192.168.1.4:9200"))// 集群缓环境下可以配置多个// HttpHost.create("http://192.168.1.7:9200")));}@AfterEachvoid teardown() throws Exception {this.client.close();}
}

初始化输出

// 初始化输出
@Test
void testInit() {System.out.println(client);
}

索引库操作

查询索引库

控制台中

控制台中添加索引库通过以下方式进行,那么在Java中也需要模拟这种请求。

需要注意的是,索引库不允许修改,只能在索引库中添加新的字段,但是不能修改

# 查询索引库
GET /bunny
Java中演示

ES遵循restfull原则,所以写几个之后大致也能猜出后面怎么写了。

// 查询索引库
@Test
void getHotelIndex() throws IOException {// 1. 创建Request对象GetIndexRequest request = new GetIndexRequest("hotel");// 2. 查询索引库boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);// 输出是否删除System.out.println(exists);
}

删除索引库

控制台中
# 删除索引库
DELETE /bunny
Java中演示
// 删除索引库
@Test
void deleteHotelIndex() throws IOException {// 1. 创建Request对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");// 2. 删除索引库client.indices().delete(request, RequestOptions.DEFAULT);
}

添加索引库

控制台中
# 创建索引库
PUT /bunny
{"mappings": {"properties": {"info": {"type": "text","analyzer": "ik_smart"},"email": {"type": "keyword","index": false},"name":{"type": "object","properties": {"firstName":{"type":"keyword"},"lastName":{"type":"keyword"}}}}}
}
Java中演示

为了简单测试,将JSON直接复制了。存到变量中。

// 添加索引库
@Test
void createHotelIndex() throws IOException {// 1. 创建Request对象CreateIndexRequest request = new CreateIndexRequest("hotel");// 2.准备请求参数,DSL语句request.source(HotelConstants.HOTEL_JSON, XContentType.JSON);// 3. 发送请求client.indices().create(request, RequestOptions.DEFAULT);
}

在这里插入图片描述

文档操作

基础结构和上面一样。

import cn.itcast.hotel.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;import java.io.IOException;public class HotelIndexTest {private RestHighLevelClient client;@BeforeEachvoid setup() throws Exception {this.client = new RestHighLevelClient(RestClient.builder((HttpHost.create("http://192.168.1.4:9200"))// 集群缓环境下可以配置多个// HttpHost.create("http://192.168.1.7:9200")));}@AfterEachvoid teardown() throws Exception {this.client.close();}
}

文档添加

控制台中

修改也是这个请求。

# 插入文档会导致版本增加
POST /bunny/_doc/1
{"info":"插入文档","email":"1@gamil.com","name":{"firstName":"舒","lastName":"纹"}
}
Java中演示
// 插入文档,记得转换成JSON对象
@Test
void testAddDocument() throws Exception {// 根据id查询酒店数据Hotel hotel = hotelService.getById(61083L);// 转换为文档类型,其中有经纬度转换HotelDoc hotelDoc = new HotelDoc(hotel);// 1. 准备Request对象IndexRequest request = new IndexRequest("hotel").id(String.valueOf(hotelDoc.getId()));// 2. 准备JSON文档request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);// 3. 发送请求client.index(request, RequestOptions.DEFAULT);
}

文档查询

控制台中

/bunny/_doc/:参数为文档名称/文档id

# 查询文档
GET /bunny/_doc/1
Java中演示

GetRequest中传入,文档名称和文档id。

// 查询文档操作
@Test
void testGetDocuments() throws Exception {// 准备RequestGetRequest request = new GetRequest("hotel", "61083");// 发送请求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 解析响应结果String json = response.getSourceAsString();System.out.println(JSON.parseObject(json, HotelDoc.class));
}

文档更新

Java中演示

request.doc中当成键值对,两两匹配。

// 更新文档
@Test
void testUpdateDocument() throws IOException {// 1. Request准备UpdateRequest request = new UpdateRequest("hotel", "61083");// 准备请求参数request.doc("price", "666","starName", "四钻");// 发送请求client.update(request, RequestOptions.DEFAULT);
}

文档删除

控制台中

/bunny/_doc/1:文档名称/_doc/文档id

# 删除文档
DELETE /bunny/_doc/1
Java中演示
// 删除文档
@Test
void testDeleteDocument() throws IOException {// 准备RequestDeleteRequest request = new DeleteRequest("hotel", "61083");// 发送请求client.delete(request, RequestOptions.DEFAULT);
}

批量插入文档

在实际中,文档操作有时是批量的,所以将数据库中所有数据都转成文档,这时候需要遍历,需要注意的是经纬度在ES中有单独的规则,在转换时,需要将经纬度转为字符串类型。

格式为:维度,经度,如:hotel.getLatitude() + ", " + hotel.getLongitude();

Java中演示
// 批量插入文档
@Test
void testBulkRequest() throws IOException {BulkRequest request = new BulkRequest();// 批量查询List<Hotel> hotels = hotelService.list();hotels.forEach(hotel -> {// 转换为HotelDocHotelDoc hotelDoc = new HotelDoc(hotel);// 创建文档的请求体IndexRequest source = new IndexRequest("hotel").id(hotel.getId().toString()).source(JSON.toJSONString(hotelDoc), XContentType.JSON);// 添加请求体request.add(source);});// 发送请求client.bulk(request, RequestOptions.DEFAULT);
}
otels.forEach(hotel -> {// 转换为HotelDocHotelDoc hotelDoc = new HotelDoc(hotel);// 创建文档的请求体IndexRequest source = new IndexRequest("hotel").id(hotel.getId().toString()).source(JSON.toJSONString(hotelDoc), XContentType.JSON);// 添加请求体request.add(source);});// 发送请求client.bulk(request, RequestOptions.DEFAULT);
}

这篇关于ES的RestClient相关操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/866087

相关文章

RecastNavigation之Poly相关类

Poly分成正常的Poly 和 OffMeshPoly。 正常的Poly 又分成 原始的Poly 和 Detail化的Poly,本文介绍这两种。 Poly的边分成三种类型: 1. 正常边:有tile内部的poly与之相邻 2.border边:没有poly与之相邻 3.Portal边:与之相邻的是外部tile的poly   由firstLink索引 得到第一个连接的Poly  通

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

SQL Server中,always on服务器的相关操作

在SQL Server中,建立了always on服务,可用于数据库的同步备份,当数据库出现问题后,always on服务会自动切换主从服务器。 例如192.168.1.10为主服务器,12为从服务器,当主服务器出现问题后,always on自动将主服务器切换为12,保证数据库正常访问。 对于always on服务器有如下操作: 1、切换主从服务器:假如需要手动切换主从服务器时(如果两个服务

JavaWeb系列二十: jQuery的DOM操作 下

jQuery的DOM操作 CSS-DOM操作多选框案例页面加载完毕触发方法作业布置jQuery获取选中复选框的值jQuery控制checkbox被选中jQuery控制(全选/全不选/反选)jQuery动态添加删除用户 CSS-DOM操作 获取和设置元素的样式属性: css()获取和设置元素透明度: opacity属性获取和设置元素高度, 宽度: height(), widt

PS的一些操作~持续抄袭中....

套索工具使用时移动图片——按住空格键,鼠标左键按住,拖动!

帆软报表常用操作

欢迎来到我的博客,代码的世界里,每一行都是一个故事 🎏:你只管努力,剩下的交给时间 🏠 :小破站 帆软报表常用操作 多序号实现使用数据集作为参数空白页或者竖线页修改页面Title金额,或者保留两位小数等等设置日期格式显示图片使用公式 多序号实现 所用函数为SEQ(),如果一张报表中需要用到多个序号,那么就需要加入参数SEQ(1),SEQ(

相关网站

力扣  https://leetcode-cn.com/contest/weekly-contest-124

CALayer相关的属性

iOS开发UI篇—CAlayer层的属性 一、position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设置CALayer在父层中的位置 以父层的左上角为原点(0, 0)   @property CGPoint anchorPoint; 称为“定位点”、“锚点”

Avalonia 常用控件二 Menu相关

1、Menu 添加代码如下 <Button HorizontalAlignment="Center" Content="Menu/菜单"><Button.Flyout><MenuFlyout><MenuItem Header="打开"/><MenuItem Header="-"/><MenuItem Header="关闭"/></MenuFlyout></Button.Flyout></B