第06讲:Java操作之ElasticSearch文档操作

该文详细介绍了如何使用Maven构建项目,引入Elasticsearch的相关依赖,然后通过RestHighLevelClient实现了Elasticsearch中的文档新增、修改、查看、删除以及批量操作,包括JSON数据的处理。
摘要由CSDN通过智能技术生成

一、创建普通Maven项目

添加pom坐标

	<dependencies>
        <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>
    </dependencies>

二、文档操作

实验1:新增文档

第1步:创建文档模型

public class User {
    private String name;
    private Integer age;
    private String sex;
    ...省略get和set...
}

第2步:创建测试类

package demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import demo.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class Demo01 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //新增文档,创建请求对象
        IndexRequest request = new IndexRequest();
        //设置索引及唯一标识
        request.index("user").id("1001");
        //创建数据对象
        User user = new User();
        user.setName("zhangsan");
        user.setAge(30);
        user.setSex("男");
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        //添加文档数据,数据格式为json格式
        request.source(productJson, XContentType.JSON);
        //客户端发送请求,获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //打印结果信息
        System.out.println("索引:"+response.getIndex());
        System.out.println("唯一标识:"+response.getId());
        System.out.println("结果:"+response.getResult());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述

实验2:修改文档

package demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import demo.entity.User;
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.common.xcontent.XContentType;

import java.io.IOException;

public class Demo02 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //修改文档,创建请求对象
        UpdateRequest request = new UpdateRequest();
        //配置修改参数
        request.index("user").id("1001");
        //设置请求体,对数据进行修改
        request.doc(XContentType.JSON, "sex", "女");
        //客户端发送请求,获取响应对象
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        //打印结果信息
        System.out.println("索引:"+response.getIndex());
        System.out.println("唯一标识:"+response.getId());
        System.out.println("结果:"+response.getResult());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述

实验3:查看文档

package demo;

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.common.xcontent.XContentType;

import java.io.IOException;

public class Demo03 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //查看文档,创建请求对象
        GetRequest request = new GetRequest();
        //配置查询参数
        request.index("user").id("1001");
        //客户端发送请求,获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //打印结果信息
        System.out.println("索引:"+response.getIndex());
        System.out.println("唯一标识:"+response.getId());
        System.out.println("文档类型:"+response.getType());
        System.out.println("文档源信息:"+response.getSourceAsString());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述

实验4:删除文档

package demo;

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;

import java.io.IOException;

public class Demo04 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //删除文档,创建请求对象
        DeleteRequest request = new DeleteRequest();
        //配置查询参数
        request.index("user").id("1001");
        //客户端发送请求,获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //打印结果信息
        System.out.println("索引:"+response.toString());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述

实验5:批量新增

package demo;

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.action.delete.DeleteResponse;
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;

import java.io.IOException;

public class Demo05 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建批量新增请求对象
        BulkRequest request = new BulkRequest();
        IndexRequest source1 = new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan");
        IndexRequest source2 = new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi");
        IndexRequest source3 = new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu");
        request.add(source1);
        request.add(source2);
        request.add(source3);
        
        //客户端发送请求,获取响应对象
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        
        //打印结果信息
        System.out.println("took:"+response.getTook());
        System.out.println("items:"+response.getItems());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述
使用postman请求:

在这里插入图片描述

实验6:批量删除

package demo;

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.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class Demo06 {

    public static void main(String[] args) throws IOException {
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建批量删除请求对象
        BulkRequest request = new BulkRequest();
        DeleteRequest source1 = new DeleteRequest().index("user").id("1001");
        DeleteRequest source2 = new DeleteRequest().index("user").id("1002");
        DeleteRequest source3 = new DeleteRequest().index("user").id("1003");
        request.add(source1);
        request.add(source2);
        request.add(source3);

        //客户端发送请求,获取响应对象
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        
        //打印结果信息
        System.out.println("took:"+response.getTook());
        System.out.println("items:"+response.getItems());

        //关闭客户端连接
        client.close();
    }
}

运行结果:

在这里插入图片描述

使用postman请求:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值