Elasticsearch7.8.0版本入门——JavaAPI操作(批量操作文档)

Posted 小志的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch7.8.0版本入门——JavaAPI操作(批量操作文档)相关的知识,希望对你有一定的参考价值。

目录

一、pom文件依赖

  • 引入相关依赖

    <!-- elasticsearch 依赖 -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.8.0</version>
    </dependency>
    <!-- elasticsearch 的客户端 -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.8.0</version>
    </dependency>
    <!-- elasticsearch 依赖 2.x 的 log4j -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.9</version>
    </dependency>
    <!-- junit 单元测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    

二、批量操作文档 代码示例

2.1、批量创建文档 代码示例

  • 批量创建文档代码示例

    package com.xz.esdemo.day3;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.common.xcontent.XContentType;
    
    /**
     * @description: 批量创建文档
     * @author: xz
     */
    public class EsDocBatchCreate 
        public static void main(String[] args) throws Exception
            // 创建 es 客户端对象
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            //创建批量新增请求对象
            BulkRequest bulkRequest = new BulkRequest();
            bulkRequest.add(new IndexRequest().index("user").id("01").source(XContentType.JSON, "name",
                "zhangsan","age",20,"sex","男"));
        	bulkRequest.add(new IndexRequest().index("user").id("02").source(XContentType.JSON, "name",
                "lisi","age",25,"sex","女"));
        	bulkRequest.add(new IndexRequest().index("user").id("03").source(XContentType.JSON, "name",
                "wangwu","age",30,"sex","女"));
        	bulkRequest.add(new IndexRequest().index("user").id("04").source(XContentType.JSON, "name",
                "wangwu1","age",18,"sex","男"));
        	bulkRequest.add(new IndexRequest().index("user").id("05").source(XContentType.JSON, "name",
                "wangwu2","age",23,"sex","女"));
    
    
            // 客户端发送请求,获取响应对象
            BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    
            //打印结果信息
            System.out.println("消耗时间took:" + response.getTook());
            System.out.println("结果信息items:" + response.getItems());
    
            // 关闭 es 客户端连接
            client.close();
        
    
    
  • 执行代码,查看控制台信息,如下图表示批量创建文档成功。

  • 通过postman工具查看批量创建的文档信息,如下图所示:

2.2、批量删除文档 代码示例

  • 批量删除文档代码示例

    package com.xz.esdemo.day3;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    
    /**
     * @description: 批量删除文档
     * @author: xz
     */
    public class EsDocBatchDelete 
        public static void main(String[] args) throws Exception
            // 创建 es 客户端对象
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            //创建批量删除请求对象
            BulkRequest bulkRequest = new BulkRequest();
            bulkRequest.add(new DeleteRequest().index("user").id("01"));
            bulkRequest.add(new DeleteRequest().index("user").id("02"));
            bulkRequest.add(new DeleteRequest().index("user").id("03"));
        	bulkRequest.add(new DeleteRequest().index("user").id("04"));
        	bulkRequest.add(new DeleteRequest().index("user").id("05"));
    
    
            // 客户端发送请求,获取响应对象
            BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    
            //打印结果信息
            System.out.println("消耗时间took:" + response.getTook());
            System.out.println("结果信息items:" + response.getItems());
    
            // 关闭 es 客户端连接
            client.close();
        
    
    
    
  • 执行代码,查看控制台信息,如下图表示批量删除文档成功。

  • 通过postman工具查看批量删除的文档信息,如下图所示:

以上是关于Elasticsearch7.8.0版本入门——JavaAPI操作(批量操作文档)的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch7.8.0版本入门—— Elasticsearch7.8.0映射操作

Elasticsearch7.8.0版本入门—— Elasticsearch7.8.0映射操作

Elasticsearch7.8.0版本入门——单机部署(linux环境-centos7)

Elasticsearch7.8.0版本入门——集群部署(linux环境-centos7)

Elasticsearch入门——Elasticsearch7.8.0版本和Kibana7.8.0版本的下载安装(win10环境)

Elasticsearch入门——Elasticsearch7.8.0版本和Kibana7.8.0版本的下载安装(win10环境)