关于文档的API操作( 更新文档和 批量插入)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 关于文档的API操作( 更新文档和 批量插入)

更新文档

package com.wyh;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyh.config.ElasticSearchClientConfig;
import com.wyh.entity.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.suggest.completion.RegexOptions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import javax.annotation.Resource;
import java.io.IOException;
/**
 * 测试es7.6.1 高级客户端API操作
**/
@SpringBootTest
class WyhApplicationTests {
//注入bean
    @Resource
private RestHighLevelClient client;
//更新文档信息
    @Test
void testUpdateDocument() throws IOException {
//更新请求
        UpdateRequest updateRequest = new UpdateRequest("wei_index","1");
//超时时间 1s
        updateRequest.timeout("1s");
//创建user对象
        User user = new User("魏二鹤", 18);
//对象转json
        String jsonString = JSON.toJSONString(user);
        updateRequest.doc(jsonString,XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse);
        System.out.println(updateResponse.status());
//UpdateResponse[index=wei_index,type=_doc,id=1,version=2,seqNo=1,primaryTerm=1,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]
        //OK
    }
}

网络异常,图片无法展示
|

查看数据库发现已经被更新成功

网络异常,图片无法展示
|

删除文档

package com.wyh;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyh.config.ElasticSearchClientConfig;
import com.wyh.entity.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.suggest.completion.RegexOptions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import javax.annotation.Resource;
import java.io.IOException;
/**
 * 测试es7.6.1 高级客户端API操作
**/
@SpringBootTest
class WyhApplicationTests {
//注入bean
    @Resource
private RestHighLevelClient client;
//删除文档信息
    @Test
void testDeleteDocument() throws IOException {
//更新请求
        DeleteRequest deleteRequest = new DeleteRequest("wei_index","1");
//超时时间 1s
        deleteRequest.timeout("1s");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse);
//DeleteResponse[index=wei_index,type=_doc,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]
    }
}

查看索引库发现文档已经被删除

网络异常,图片无法展示
|

批量插入

package com.wyh;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyh.config.ElasticSearchClientConfig;
import com.wyh.entity.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.suggest.completion.RegexOptions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
/**
 * 测试es7.6.1 高级客户端API操作
**/
@SpringBootTest
class WyhApplicationTests {
//注入bean
    @Resource
private RestHighLevelClient client;
//特殊的 ElasticSearch批量插入数据
    @Test
void testBatchInsert() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
//设置过期时间
        bulkRequest.timeout("10s");
//泛型为User的list
        ArrayList<User> userArrayList = new ArrayList<>();
//添加用户信息
        userArrayList.add(new User("魏一鹤1",14));
        userArrayList.add(new User("魏一鹤2",14));
        userArrayList.add(new User("魏一鹤3",14));
        userArrayList.add(new User("魏一鹤4",14));
        userArrayList.add(new User("魏一鹤5",14));
//批量添加数据
        for (int i = 0; i < userArrayList.size(); i++) {
//批量删除或者插入就在这里修改对应的请求即可
            bulkRequest.add(
//插入那个库
                    new IndexRequest("wei_index")
//id
                    .id(""+(i+1))
//json数据源
                    .source(JSON.toJSONString(userArrayList.get(i)),XContentType.JSON)
            );
        }
//执行批量插入请求并且获取相应
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否执行失败 如果是false就没有失败
        System.out.println(bulk.hasFailures());//false
    }
}

查看库里发现已经插入成功

网络异常,图片无法展示
|

如果指名id会随机生成很长的id,防止数据重复

网络异常,图片无法展示
|

网络异常,图片无法展示
|

相关实践学习
利用Elasticsearch实现地理位置查询
本实验将分别介绍如何使用Elasticsearch7.10版本进行全文检索、多语言检索和地理位置查询三个Elasticsearch基础检索子场景的实现。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
7天前
|
Java API 开发者
在Spring Boot中集成Swagger API文档
在Spring Boot中集成Swagger API文档
|
2天前
|
搜索推荐 API UED
资源部署及场景API调用体验过程的引导与操作流畅性
资源部署及场景API调用体验过程的引导与操作流畅性
|
6天前
|
开发框架 Java 测试技术
Spring Boot中的API文档生成
Spring Boot中的API文档生成
|
12天前
|
JSON Java API
Spring Boot中使用OpenAPI生成API文档
Spring Boot中使用OpenAPI生成API文档
|
17天前
|
运维 DataWorks 数据管理
DataWorks操作报错合集之调用RegisterLineageRelation api时报错,是什么原因?
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
18 2
|
5天前
|
并行计算 Java 数据挖掘
Java面试题:解释Java中的Stream API及其操作
Java面试题:解释Java中的Stream API及其操作
10 0
|
12天前
|
API 开发工具
支付系统17------支付宝支付-----API预览以及签名验签说明,出现支付宝扫描二维码的操作,支付完成之后,查询订单的状态,支付成功之后,需要退款调用的接口,退款状态的接口,完成退款之后,通知
支付系统17------支付宝支付-----API预览以及签名验签说明,出现支付宝扫描二维码的操作,支付完成之后,查询订单的状态,支付成功之后,需要退款调用的接口,退款状态的接口,完成退款之后,通知
|
17天前
|
SQL DataWorks 监控
DataWorks操作报错合集之在调用数据服务API时返回的错误码是"ODPS-0410051",并且错误信息提示"Invalid credentials - accessKeyId not found",该怎么办
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
14 0
|
17天前
|
移动开发 监控 Serverless
函数计算操作报错合集之在调用api时,遇到404问题该怎么办
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
|
17天前
|
Java API Spring
Spring Boot中配置Swagger用于API文档
Spring Boot中配置Swagger用于API文档