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

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

java 复制代码
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

修改数据的代码如下:

java 复制代码
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的数据代码如下:

java 复制代码
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();
    }
}

执行结果如下:

删除指定文档的代码如下

java 复制代码
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();
    }
}

执行显示结果如下

相关推荐
LKAI.2 小时前
搭建Elastic search群集
linux·运维·elasticsearch·搜索引擎
青木沐4 小时前
Jenkins介绍
运维·jenkins
WTT00115 小时前
2024楚慧杯WP
大数据·运维·网络·安全·web安全·ctf
只会copy的搬运工8 小时前
Jenkins 持续集成部署——Jenkins实战与运维(1)
运维·ci/cd·jenkins
云云3219 小时前
怎么通过亚矩阵云手机实现营销?
大数据·服务器·安全·智能手机·矩阵
新加坡内哥谈技术9 小时前
苏黎世联邦理工学院与加州大学伯克利分校推出MaxInfoRL:平衡内在与外在探索的全新强化学习框架
大数据·人工智能·语言模型
Data-Miner10 小时前
经典案例PPT | 大型水果连锁集团新零售数字化建设方案
大数据·big data
lovelin+v1750304096610 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析
测试杂货铺10 小时前
如何用postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·测试工具·职场和发展·jenkins·压力测试·postman