平潭县建设局网站,logo设计在线生成免费u钙,网站站点建设的端口,.net网站开发源码注释前言#xff1a;上文介绍了使用DSL语言操作索引库和文档#xff0c;本篇文章将介绍使用Java中的RestClient来对索引库和文档进行操作。 希望能够加深自己的印象以及帮助到其他的小伙伴儿们#x1f609;#x1f609;。 如果文章有什么需要改进的地方还请大佬不吝赐教#x…前言上文介绍了使用DSL语言操作索引库和文档本篇文章将介绍使用Java中的RestClient来对索引库和文档进行操作。 希望能够加深自己的印象以及帮助到其他的小伙伴儿们。 如果文章有什么需要改进的地方还请大佬不吝赐教。 小威在此先感谢各位大佬啦~~ 个人主页小威要向诸佬学习呀 个人简介大家好我是小威一个想要与大家共同进步的男人 目前状况24届毕业生曾经在某央企公司实习目前在某税务公司实习 欢迎大家这里是CSDN我总结知识的地方欢迎来到我的博客我亲爱的大佬 以下正文开始
文章目录RestClient操作索引库创建索引库删除索引库RestClient操作文档插入文档查询文档修改文档好那就详细记录下这块的知识。
前面记录了在网页端使用DSL语句对Elasticsearch的索引库和文档进行增删改查的简单操作。但是在日常的开发工作中还是用Java语言操作比较多因此需要使用Elasticsearch官方提供的RestClient操作索引库和文档。
首先准备一个索引库名为hotel的库并分析其中字段的数据结构然后根据字段的名称数据类型是否参与搜索是否分词分词器等条件来完善其mapping在其内部定义了一个名为“all”字段的属性这个字段目的是将其他同时参与搜索的字段cope_to在一起搜索的时候根据“all”字段内的查询条件一起搜索可以提高搜索效率
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}}}
}那么如何在idea中操作对索引库和文档进行操作呢
倘若我们想要使用RestClient来操作首要任务就是引入其依赖 !--elasticsearch--dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.12.1/version/dependencydependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactId/dependency第二步在测试类中编写测试方法我们需要创建RestClient对象然后对RestClient进行初始化当然创建完成RestClient后需要销毁代码如下 private RestHighLevelClient client;BeforeEach //创建对象初始化void setUp() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.220.***:9200) //创建方法));}AfterEach //创建完成后销毁void tearDown() throws IOException {client.close();}RestClient操作索引库
创建索引库
这里的创建索引库方法和我们上面DSL语句的含义是一样的虽然描述方式有所不同。
第一行代码创建索引库相当于DSL语句中的PUT /hotel第二行代码准备请求参数MAPPING_TEMPLATE为创建DSL语句中的内容除去PUT /hotel那些第三步调用client的indices()拿到操作索引库的所有方法然后取出create方法 Testvoid testCreateIndex() throws IOException {// 1.准备Request PUT /hotelCreateIndexRequest request new CreateIndexRequest(hotel);// 2.准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);//这里将DSL语句封装成了MAPPING_TEMPLATE优雅美观// 3.发送请求client.indices().create(request, RequestOptions.DEFAULT);}可以进入indices方法的源码查看得返回的是indicesClient如下
public final IndicesClient indices() {return this.indicesClient;
}delete的方法的源码由此可见上面传入request请求即可
public final class IndicesClient {private final RestHighLevelClient restHighLevelClient;IndicesClient(RestHighLevelClient restHighLevelClient) {this.restHighLevelClient restHighLevelClient;}public AcknowledgedResponse delete(DeleteIndexRequest deleteIndexRequest, RequestOptions options) throws IOException {return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, IndicesRequestConverters::deleteIndex, options,AcknowledgedResponse::fromXContent, emptySet());}
}删除索引库
根据以上信息我们不难得出使用RestClient删除索引库和判断索引库是否存在的相关代码 Test //判断索引库是否存在void testExistsIndex() throws IOException {// 1.准备Request注意这块是获取索引库请求而不是创建GetIndexRequest request new GetIndexRequest(hotel);// 3.发送请求调用exists方法boolean isExists client.indices().exists(request, RequestOptions.DEFAULT);System.err.println(isExists ? 索引库存在 : 索引库不存在);}Test //删除索引库操作void testDeleteIndex() throws IOException {// 1.准备Request指定删除哪个索引库DeleteIndexRequest request new DeleteIndexRequest(hotel); // 3.发送请求调用delete方法client.indices().delete(request, RequestOptions.DEFAULT);}综上所述索引库操作的基本步骤 • 初始化RestHighLevelClient • 创建XxxIndexRequest。Xxx可以是CreateGetDelete • 准备DSL语句 Create时需要 • 发送请求。调用RestHighLevelClient#indices().xxx()方法xxx可以是createexistsdelete
RestClient操作文档
操作文档和操作索引库一样需要完成RestClient的初始化和销毁操作这里不展现重复代码了。
插入文档
前面调用MybatisPlus中查询的方法从数据库中查询出ID为61083的信息由于索引库和数据库中的某字段不是很对应所以做了一次转换。之后开始操作文档。
第一步创建文档与POST /索引库名称/_doc/1相对应第二步准备json文档上部代码已经对数据json序列化了第三步直接调用index方法发送请求 private RestHighLevelClient client;Autowiredprivate IHotelService hotelService;Testvoid testAddDocument() throws IOException {// 1.查询数据库hotel数据Hotel hotel hotelService.getById(61083L);// 2.转换为HotelDocHotelDoc hotelDoc new HotelDoc(hotel);// 3.转JSONString json JSON.toJSONString(hotelDoc);// 1.准备RequestIndexRequest request new IndexRequest(hotel).id(hotelDoc.getId().toString());//索引库中对id的要求为keyword因此要转换成string类型// 2.准备请求参数DSL其实就是文档的JSON字符串request.source(json, XContentType.JSON);// 3.发送请求client.index(request, RequestOptions.DEFAULT);}查询文档
根据id查询文档信息相对应的DSL语句为GET /数据库名称/_doc/1。查询是新建GetRequest对象。 Testvoid testGetDocumentById() throws IOException {// 1.准备Request // GET /hotel/_doc/{id}GetRequest request new GetRequest(hotel, 61083);// 2.发送请求GetResponse response client.get(request, RequestOptions.DEFAULT);// 3.解析响应结果String json response.getSourceAsString();//查询出来的对象是json形式这里转换成HotelDoc对象形式HotelDoc hotelDoc JSON.parseObject(json, HotelDoc.class);System.out.println(hotelDoc hotelDoc);}修改文档
前面介绍的修改有两种方式一种为全局修改一种为增量修改这里以增量修改为例DSL语句POST /数据库名称/_doc/1 Testvoid testUpdateById() throws IOException {// 1.准备RequestUpdateRequest request new UpdateRequest(hotel, 61083);// 2.准备参数,这里是需要改变的参数request.doc(price, 870);// 3.发送请求client.update(request, RequestOptions.DEFAULT);}
文档操作的基本步骤 • 初始化RestHighLevelClient • 创建XxxRequest。Xxx可以是IndexGetUpdateDelete • 准备参数Index和Update时需要 • 发送请求。调用RestHighLevelClient.xxx()方法xxx可以是indexgetupdatedelete • 解析结果Get时需要将查询出的json形式转化为对象形式
本篇文章就先分享到这里了后续会继续分享其他方面的知识感谢大佬认真读完支持咯~ 文章到这里就结束了如果有什么疑问的地方请指出诸佬们一起讨论 希望能和诸佬们一起努力今后我们顶峰相见 再次感谢各位小伙伴儿们的支持