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();
    }
}

执行显示结果如下

相关推荐
涛思数据(TDengine)14 分钟前
TDengine 数据订阅 vs. InfluxDB 数据订阅:谁更胜一筹?
大数据·时序数据库·tdengine
MXsoft61843 分钟前
监控易监测对象及指标之:Canal中间件监控
大数据·数据库
Mephisto.java3 小时前
【大数据学习 | kafka】kafka的整体框架与数据结构
大数据·学习
码农易小航3 小时前
封装ES高亮Yxh-Es
大数据·elasticsearch·搜索引擎
TechCraftsman数据库专栏3 小时前
为什么需要 ElasticSearch
数据库·elasticsearch
小小工匠3 小时前
ElasticSearch - Bucket Script 使用指南
elasticsearch·bucket script
samFuB3 小时前
上市公司企业数字金融认知数据集(2001-2023年)
大数据·金融
程序员爱中国3 小时前
Elasticsearch - 基础入门篇
大数据·elasticsearch·搜索引擎
刘圆辉4 小时前
DataSophon集成ApacheImpala的过程
大数据·impala·datasophon·apache impala
guanpinkeji5 小时前
剧本杀门店预约小程序,在线一键预约体验
大数据·小程序·团队开发·软件开发