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

执行显示结果如下

相关推荐
Loving_enjoy2 分钟前
基于Hadoop的明星社交媒体影响力数据挖掘平台:设计与实现
大数据·hadoop·数据挖掘
浮尘笔记9 分钟前
go-zero使用elasticsearch踩坑记:时间存储和展示问题
大数据·elasticsearch·golang·go
碳基学AI1 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义免费下载方法
大数据·人工智能·python·gpt·算法·语言模型·集成学习
unhurried人生——冕临2 小时前
Ubuntu安装Elasticsearch
elasticsearch
一个天蝎座 白勺 程序猿2 小时前
大数据(4.6)Hive执行引擎选型终极指南:MapReduce/Tez/Spark性能实测×万亿级数据资源配置公式
大数据·hive·mapreduce
HelpHelp同学3 小时前
信息混乱难查找?三步搭建高效帮助中心解决难题
大数据·人工智能·知识库管理系统
TDengine (老段)9 小时前
TDengine 中的关联查询
大数据·javascript·网络·物联网·时序数据库·tdengine·iotdb
这个懒人11 小时前
深入解析Translog机制:Elasticsearch的数据守护者
数据库·elasticsearch·nosql·translog
直裾13 小时前
Mapreduce的使用
大数据·数据库·mapreduce
LCY13315 小时前
spring 中的DAO是什么
运维·git·jenkins