Elasticsearch实现增删改查

调用elasticsearch通常使用restful风格请求,这里记录一些常用的Java API和Postman Url

Java API调用Es

1. 查询总文档数

java 复制代码
@Test
    void getAllCount() {
//        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.3",9200,"http")));
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.1",9200,"http")));
        SearchRequest searchRequest=new SearchRequest();
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.indices("_all");
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse searchResponse=client.search(searchRequest, RequestOptions.DEFAULT);
            long count=searchResponse.getHits().getTotalHits().value;
            System.out.println("192.168.32.3的文档总数量为: "+count);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

2.批量插入100w数据

java 复制代码
    @Test
    public void setESBatchData() throws IOException {
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.2",9200,"http")));
        BulkRequest request=new BulkRequest();
        request.timeout("10m");
        long startTime = System.currentTimeMillis();
        for (int i = 1; i < 1000000; i++) {
//            Map<String, String> jsonMap = new HashMap<>();
            String name="test"+i;
            int age=i;
//            jsonMap.put("name", "test"+i);
//            jsonMap.put("age", String.valueOf(i));
//            String jsonString = formatAsJSON(jsonMap);
            IndexRequest indexRequest=new IndexRequest("my_index").source("name",name,"age",age);
            request.add(indexRequest);
            if(i%10000==0){
                BulkResponse response=client.bulk(request,RequestOptions.DEFAULT);
                request=new BulkRequest();
                if (response.hasFailures()) {
                    // 处理失败的文档
                    System.out.println("第"+i/10000+"次批量插入失败");
                } else {
                    System.out.println("第"+i/10000+"次批量插入成功,已插入"+i/10000+"w条");
                    // 所有文档都已成功插入
                }
            }
        }
        long endTime = System.currentTimeMillis();
        double timeDifference = (endTime - startTime)/1000;
        System.out.println("两次系统时间差:" + timeDifference + " 秒");
//        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.setHttpAsyncResponseConsumerFactory(new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(500 * 1024 * 1024));
//        RequestConfig config=RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(600000).build();
//        RequestOptions options=builder.setRequestConfig(config).build();
    }

3.关闭指定索引

java 复制代码
    @Test
    public void closeESIndex() throws IOException {
        //创建es客户端,建立连接
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.1",9200,"http")));
//        String indexName=".ds-ilm-history-5-2023.06.29-000001";
        String indexName=".ds-.logs-deprecation.elasticsearch-default-2023.06.29-000001";
//        String indexName="my_index";
        //根据操作类型创建请求
        CloseIndexRequest request=new CloseIndexRequest(indexName);
        //执行请求,返回结果
        AcknowledgedResponse response=client.indices().close(request,RequestOptions.DEFAULT);
        if (response.isAcknowledged()){
            System.out.println("索引关闭成功");
        }else {
            System.out.println("索引关闭失败");
        }
        //释放连接资源
        client.close();
    }

4.添加文档

java 复制代码
@Test
    public void addES() throws IOException {
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.3",9200,"http")));
        String jsonDocument = "{ \"name\": \"test14\", \"age\": \"140\" }";
        IndexRequest request=new IndexRequest("my_index").id("14").source(jsonDocument, XContentType.JSON);
        IndexResponse response=client.index(request,RequestOptions.DEFAULT);
        if(response.getResult()== IndexResponse.Result.CREATED){
            System.out.println("文档创建成功");
        }else if(response.getResult()==IndexResponse.Result.UPDATED){
            System.out.println("文档更新成功");
        }
        client.close();
    }

5.修改文档

java 复制代码
 @Test
    public void updateES() throws IOException {
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.3",9200,"http")));
        UpdateRequest request=new UpdateRequest("my_index","_doc","14");
        Map<String,String> updateInfo=new HashMap<>();
        request.doc("name","test14_update");
        UpdateResponse response=client.update(request,RequestOptions.DEFAULT);
        if(response.getResult()== DocWriteResponse.Result.UPDATED){
            System.out.println("修改文档成功");
        }
        client.close();
    }

6.删除文档

java 复制代码
 @Test
    public void deleteES() throws IOException {
        RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.32.3",9200,"http")));
        DeleteRequest request=new DeleteRequest("my_index","14");
        DeleteResponse response=client.delete(request,RequestOptions.DEFAULT);
        if(response.getResult()== DocWriteResponse.Result.DELETED){
            System.out.println("删除文档成功");
        }
    }

Postman请求(DSL)

1.查询全量文档

java 复制代码
POST http://192.168.32.2:9201/_search?pretty
{
    "size": 1000,
    "query": {
        "match_all": {}
    }
}

2.删除全部文档(保留索引)

java 复制代码
POST http://192.168.32.2:9201/_all/_delete_by_query
{
    "query": {
        "match_all": {}
    }
}

3.新增文档

java 复制代码
PUT http://192.168.32.2:9201/my_index/_doc/1
{
    "name": "test1",
    "age": 1
}

4.查看x-pack许可证信息

java 复制代码
GET http://192.168.32.2:9201/_xpack?pretty

5.查看集群节点

java 复制代码
GET http://192.168.32.2:9200/_cat/nodes?v

6.查看节点信息

java 复制代码
GET http://192.168.32.1:9201

7.查询文档总数

java 复制代码
GET http://192.168.32.2:9201/_search?pretty
{
    "size": 0,
    "aggs": {
        "total_documents": {
            "value_count": {
                "field": "_id"
            }
        }
    }
}

8.创建索引

java 复制代码
PUT http://192.168.32.2:9200/test_index
{
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            },
            "age": {
                "type": "long"
            }
        }
    }
}

9.删除索引(包括数据)

java 复制代码
DELETE http://192.168.32.2:9201/my_index

10.查询集群索引

java 复制代码
GET http://192.168.32.2:9201/_cat/indices?v

11.查看指定索引结构

java 复制代码
GET http://192.168.32.2:9200/my_index/_mapping

12.开启指定索引

java 复制代码
POST http://192.168.32.2:9201/my_index/_open

13.关闭指定索引

java 复制代码
POST http://192.168.32.2:9201/my_index/_close

14.定义远程集群

java 复制代码
POST http://192.168.32.2:9201/my_index/_close

15.查看远程集群

java 复制代码
GET http://192.168.32.2:9201/_remote/info

16.删除远程集群

java 复制代码
PUT http://192.168.32.2:9201/_cluster/settings
{
  "persistent": {
    "cluster": {
      "remote": {
        "es-source": {
          "seeds": null 
        }
      }
    }
  }
}

17.开启CCR(指定索引)

java 复制代码
PUT http://192.168.32.2:9201/my_index/_ccr/follow
{
    "remote_cluster": "source",
    "leader_index": "my_index"
}

18.暂停CCR

java 复制代码
POST http://192.168.32.2:9201/my_index/_ccr/pause_follow

19.删除CCR

java 复制代码
POST http://192.168.32.2:9201/my_index/_ccr/unfollow

删除索引也会自动删除CCR

20.查看CCR信息

java 复制代码
POST http://192.168.32.2:9201/my_index/_ccr/info

21.自动跟随增量同步索引(auto follow)

java 复制代码
PUT http://192.168.32.2:9201/_ccr/auto_follow/beats
{
    "remote_cluster": "source",
    "leader_index_patterns": [
        "*"
    ]
}
相关推荐
QT 小鲜肉3 分钟前
【Linux命令大全】001.文件管理之lsattr命令(实操篇)
linux·运维·服务器·笔记·elasticsearch
许泽宇的技术分享25 分钟前
2025年度技术之旅:在AI浪潮下的个人突破、持续创作与平衡之道
大数据·人工智能
Sui_Network39 分钟前
智能体支付时代:Sui 为 AI 构建可验证的金融基础设施
大数据·人工智能·游戏·金融·rpc·区块链·量子计算
GEO AI搜索优化助手44 分钟前
生成式AI搜索的跨行业革命与商业模式重构
大数据·人工智能·搜索引擎·重构·生成式引擎优化·ai优化·geo搜索优化
武子康1 小时前
大数据-198 KNN 必须先归一化:Min-Max 正确姿势、数据泄露陷阱与 sklearn 落地
大数据·后端·机器学习
递归尽头是星辰1 小时前
Elasticsearch实战:检索优化、聚合分析与架构落地体系化
大数据·elasticsearch·架构·检索优化·聚合分析
Dxy12393102161 小时前
Elasticsearch 8.13.4 动态同义词实战全解析
大数据·elasticsearch
芝麻开门-新起点1 小时前
第24章-WebGIS发布与在线分析
大数据
qq_12498707531 小时前
基于微信小程序的科技助农系统的设计与实现(源码+论文+部署+安装)
java·大数据·spring boot·后端·科技·微信小程序·毕业设计
AC赳赳老秦2 小时前
农业智能化:DeepSeek赋能土壤与气象数据分析,精准预测病虫害,守护丰收希望
java·前端·mongodb·elasticsearch·html·memcache·deepseek