Elasticsearch7.8.0版本入门——JavaAPI操作(索引操作)

一、创建索引代码示例

  • 创建索引代码示例

    package com.xz.esdemo.day1;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.CreateIndexRequest;
    import org.elasticsearch.client.indices.CreateIndexResponse;
    
    import java.io.IOException;
    
    /**
     * @description: 创建索引
     * @author: xz
     */
    public class EsIndexCreate {
        public static void main(String[] args) throws IOException {
            // 创建 es 客户端对象
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 创建索引 - 请求对象
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("role");
            // 发送请求,获取响应
            CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            // 响应状态
            boolean acknowledged = createIndexResponse.isAcknowledged();
            System.out.println("响应状态===="+acknowledged);
    
            // 关闭 es 客户端连接
            client.close();
        }
    }
    
  • 执行代码,查看控制台信息
    在这里插入图片描述

  • 使用postman工具查看创建索引是否成功,如下图表示JavaAPI操作创建索引成功
    在这里插入图片描述

二、查询索引代码示例

  • 查询索引代码示例

    package com.xz.esdemo.day1;
    
    import org.apache.http.HttpHost;
    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 java.io.IOException;
    
    /**
     * @description: 查询索引
     * @author: xz
     */
    public class EsIndexSearch {
        public static void main(String[] args) throws IOException {
            // 创建 es 客户端对象
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 查询索引 请求对象
            GetIndexRequest request = new GetIndexRequest("role");
            // 发送请求,获取响应
            GetIndexResponse response = client.indices().get(request,
                    RequestOptions.DEFAULT);
    
            System.out.println("aliases:"+response.getAliases());
            System.out.println("mappings:"+response.getMappings());
            System.out.println("settings:"+response.getSettings());
    
            // 关闭 es 客户端连接
            client.close();
        }
    }
    
  • 执行代码,查看控制台信息
    在这里插入图片描述

二、删除索引代码示例

  • 删除索引代码示例

    package com.xz.esdemo.day1;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.support.master.AcknowledgedResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import java.io.IOException;
    
    /**
     * @description: 删除索引
     * @author: xz
     */
    public class EsIndexDelete {
        public static void main(String[] args) throws IOException {
            // 创建 es 客户端对象
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );
    
            // 删除索引 请求对象
            DeleteIndexRequest request = new DeleteIndexRequest("role");
            // 发送请求,获取响应
            AcknowledgedResponse delete = client.indices().delete(request,
                    RequestOptions.DEFAULT);
    
            System.out.println("acknowledged:"+delete.isAcknowledged());
    
            // 关闭 es 客户端连接
            client.close();
        }
    }
    
  • 执行代码,查看控制台信息
    在这里插入图片描述

  • 再使用postman工具查看删除索引是否成功,如下图表示JavaAPI操作删除索引成功。
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小志的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值