栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RestClient实现Elasticsearch的简单操作

RestClient实现Elasticsearch的简单操作

    @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":3n" + "   },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());
            }
        }
    }

    
    @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();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号