当前位置: 首页 > 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.mrgr.cn/news/50876.html

相关文章:

  • 当代社恐青年的社交解药:跟AI唠嗑上瘾
  • Crawl4AI:用几行代码打造强大的网页爬虫
  • C++运算出现整型溢出
  • ros service不走是为什么
  • “我们为什么缺少科学精神”演讲内容拆解
  • live2d 实时虚拟数字人形象页面显示,对接大模型
  • 机器学习:知识蒸馏(Knowledge Distillation,KD)
  • HTTP状态码
  • java代码编译javac
  • 如何让c盘不是那么快速的红
  • 信创服务器下连接kingbase并执行SQL的脚本
  • 数据可视化-使用python制作词云图(附代码)
  • 植物大战僵尸杂交版之后新作:植物大战僵尸射击版(可在安卓手机上玩,文末附下载链接)
  • 活动报名丨实时互动AI 开发者年度聚会!RTE Open Day@RTE2024,10.25/26北京
  • IOS APP初体验-第1课:如何在Mac中真机调试
  • |动漫爬取|001_djangodjango基于Spark的国漫推荐系统的设计与实现2024_tpd6q1o4
  • onnx和tensorrt使用过程中的一些代码梯子
  • 单链表算法题(一)(超详细版)
  • 基于SpringBoot+Vue+MySQL的养老保险管理系统
  • C1. Adjust The Presentation (Easy Version) 双指针