RestClient实现Elasticsearch的简单操作

本文提供了一系列Elasticsearch操作的示例代码,包括创建索引、查询索引信息、删除索引等,并展示了如何使用Java进行REST API调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    @Test
    public void createIndex() throws IOException {
        Request getRequest = new Request("put", "/20220327001/");
        getRequest.setJsonEntity("{\n" + "   \"settings\":{\n" + "       \"number_of_shards\":5,\n" + "       \"number_of_replicas\":3\n" + "   },\n" + "   \"mappings\":{\n" + "\n" + "           \"properties\":{\n" + "               \"name\":{\n" + "                   \"type\":\"keyword\"\n" + "               }\n" + "\n" + "       }\n" + "   }\n" + "}");

        RestClient client = EsConfig.initClient();
        Response response = client.performRequest(getRequest);
        client.close();

        System.out.println(EntityUtils.toString(response.getEntity()));

    }

    /**
     * 查询所有的索引信息
     */
    @Test
    public void searchIndices() {
        RestClient client = EsConfig.initClient();

        Request request = new Request("GET", "/_cat/indices?v");

        try {
            Response response = client.performRequest(request);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException exception) {
            LOGGER.info(exception.getMessage());
        } finally {
            try {
                client.close();
            } catch (IOException exception) {
                LOGGER.info(exception.getMessage());
            }
        }
    }

    /**
     * 根据索引id查询索引信息
     */
    @Test
    public void searchIndexById() {
        RestClient client = EsConfig.initClient();
        Request request = new Request("Get", "/20220325001/");
        try {
            Response response = client.performRequest(request);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException exception) {
            LOGGER.info(exception.getMessage());
        } finally {
            try {
                client.close();
            } catch (IOException exception) {
                LOGGER.info(exception.getMessage());
            }
        }

    }

    /**
     * 删除索引
     */
    @Test
    public void deleteIndex() {
        RestClient client = EsConfig.initClient();
        Request request = new Request("delete", "/20220325003");

        Cancellable cancellable = client.performRequestAsync(request, new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                try {
                    LOGGER.info(EntityUtils.toString(response.getEntity()));
                } catch (IOException e) {
                    LOGGER.info(e.getMessage());
                }
            }

            @Override
            public void onFailure(Exception e) {
                System.out.println(e.getMessage());
            }
        });
    }

    @Test
    public void query() throws IOException {
        RestClient client = EsConfig.initClient();
        Request request = new Request("GET", "20220325001/_doc/20220326001");
//        StringBuilder body = new StringBuilder();
        Response response = client.performRequest(request);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    @Test
    public void queryMatch() throws IOException {
        RestClient client = EsConfig.initClient();
        Request request = new Request("POST", "/20220325001/_search?filter_path=hits");
        request.setJsonEntity(" {\n" +
                "    \"query\":{\n" +
                "        \"match_all\":{\n" +
                "            \n" +
                "        }\n" +
                "    }\n" +
                "}");
        Response response = client.performRequest(request);
        System.out.println(EntityUtils.toString(response.getEntity()));
        client.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值