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

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.kler.cn/news/355747.html

相关文章:

  • 【芙丽芳丝净润洗面霜和雅漾舒护活泉喷雾
  • AnaTraf | TCP重传的工作原理与优化方法
  • 【数据分享】1901-2023年我国省市县三级逐月最低气温(免费获取/Shp/Excel格式)
  • 详解tcpdump
  • (4) cuda cudnn TensorRT安装及配置
  • Qt(信号槽)
  • 等保测评与网络安全应急响应
  • uni-app 实现好看易用的抽屉效果
  • 实时计算Flink应用场景
  • PMP--必刷题–解题–161-172
  • python编程:常用模块分类整理
  • 打包使用pythn编写的maya插件,使用pyeal打包
  • Karmada核心概念
  • beyond compare 这个授权密钥已被吊销
  • Lucene 倒排索引
  • 5.深度学习计算
  • 【MR开发】在Pico设备上接入MRTK3(二)——在Unity中配置Pico SDK
  • 利用Arcgis进行沟道形态分析
  • maven工程怎么将除工程源码外的三方依赖一起打包到jar中
  • Vue3 集成Monaco Editor编辑器