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

ES-入门-javaApi-文档-新增-删除

新增指定索引的文档数据的代码如下:

package com.atgulgu.es.test;import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.xcontent.XContentType;/*** ClassName: ESTest_Client* Package: com.atgulgu.es.test** @Author 杨小燕* @Create 2024/10/7 0007 23:37* @Version 1.0*/
public class ESTest_Doc_Insert {public static void main(String[] args)  throws  Exception {//创建ES客户单RestHighLevelClient esClient =  new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.108",9200,"http")));//插入数据IndexRequest  request = new IndexRequest();request.index("user").id("1001");User  user = new User();user.setName("zhangsan");user.setAge(30);user.setSex("男");//向ES插入数据,必须将数据转换为JSOn格式ObjectMapper  mapper = new ObjectMapper();String userjson = mapper.writeValueAsString(user);request.source(userjson, XContentType.JSON);IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);System.out.println(response.getResult());//关闭ES客户端esClient.close();}
}

执行成功会提示created

修改数据的代码如下:

package com.atgulgu.es.test;import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;/*** ClassName: ESTest_Client* Package: com.atgulgu.es.test** @Author 杨小燕* @Create 2024/10/7 0007 23:37* @Version 1.0*/
public class ESTest_Doc_update {public static void main(String[] args)  throws  Exception {//创建ES客户单RestHighLevelClient esClient =  new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.108",9200,"http")));//修改数据UpdateRequest request = new UpdateRequest();request.index("user").id("1001");request.doc(XContentType.JSON,"sex","女");UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);System.out.println(response.getResult());//关闭ES客户端esClient.close();}
}

查询ES中具体索引和ID的数据代码如下:

package com.atgulgu.es.test;import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;/*** ClassName: ESTest_Client* Package: com.atgulgu.es.test** @Author 杨小燕* @Create 2024/10/7 0007 23:37* @Version 1.0*/
public class ESTest_Doc_Get {public static void main(String[] args)  throws  Exception {//创建ES客户单RestHighLevelClient esClient =  new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.108",9200,"http")));//查询数据GetRequest request = new GetRequest();request.index("user").id("1001");GetResponse response = esClient.get(request, RequestOptions.DEFAULT);System.out.println(response.getSourceAsString());//关闭ES客户端esClient.close();}
}

执行结果如下:

删除指定文档的代码如下

package com.atgulgu.es.test;import org.apache.http.HttpHost;
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.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;/*** ClassName: ESTest_Client* Package: com.atgulgu.es.test** @Author 杨小燕* @Create 2024/10/7 0007 23:37* @Version 1.0*/
public class ESTest_Doc_Delete {public static void main(String[] args)  throws  Exception {//创建ES客户单RestHighLevelClient esClient =  new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.1.108",9200,"http")));//删除文档数据DeleteRequest request = new DeleteRequest();request.index("user").id("1001");DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);System.out.println(response.toString());//关闭ES客户端esClient.close();}
}

执行显示结果如下

http://www.lryc.cn/news/464011.html

相关文章:

  • 【视频生成大模型】 视频生成大模型 THUDM/CogVideoX-2b
  • 【MR开发】在Pico设备上接入MRTK3(三)——在Unity中运行MRTK示例
  • C#中委托的应用与示例
  • 算法: 模拟题目练习
  • 软考中级科目怎么选?软考中级证书有什么用?
  • HTTP 请求的请求体是什么
  • 助力语音技术发展,景联文科技提供语音数据采集服务
  • PyTorch搭建神经网络入门教程
  • 你的电脑能不能安装windows 11,用这个软件检测下就知道了
  • BF 算法
  • SHOW-O——一款结合多模态理解和生成的单一Transformer
  • 缓存框架JetCache源码解析-缓存变更通知机制
  • Android 设置特定Activity内容顶部显示在状态栏底部,也就是状态栏的下层 以及封装一个方法修改状态栏颜色
  • 用自己的数据集复现YOLOv5
  • 如何在博客中插入其他的博客链接(超简单)最新版
  • JS通过递归函数来剔除树结构特定节点
  • javayufa
  • 软考-高级系统分析师知识点-补充篇
  • JavaScript全面指南(四)
  • 2024年诺贝尔物理学奖的创新之举
  • FileLink内外网文件交换——致力企业高效安全文件共享
  • 使用Python在Jupyter Notebook中显示Markdown文本
  • G1 GAN生成MNIST手写数字图像
  • WPFDeveloper正式版发布
  • 实现鼠标经过某个元素时弹出提示框(通常称为“工具提示”或“悬浮提示”)
  • 【GAMES101笔记速查——Lecture 17 Materials and Appearances】
  • 对于从vscode ssh到virtualBox的timeout记录
  • 鸿蒙原生应用扬帆起航
  • 《计算机视觉》—— 表情识别
  • NVIDIA Aerial Omniverse