Java中ElasticSearch的删除示例

public class DeleteElasticAPI {
    private static RestClient restClient;

    static {
        restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
    }

    /**
     * 创建文档
     * @throws Exception
     */
    @Test
    public void CreateDocument()throws Exception{

        String method = "PUT";
        String endpoint = "/delete-index/test/2";
        HttpEntity entity = new NStringEntity(
                "{\n" +
                        "    \"user\" : \"大美女\",\n" +
                        "    \"post_date\" : \"2009-02-15T14:12:12\",\n" +
                        "    \"message\" : \"trying begin Elasticsearch\"\n" +
                        "}", ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 获取文档
     * @throws Exception
     */
    @Test
    public void getDocument()throws Exception{
        String method = "GET";
        String endpoint = "/delete-index/test/1";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 删除文档
     * @throws IOException
     */
    @Test
    public void delete () throws IOException {
        String method = "DELETE";
        String endpoint = "/delete-index/test/1";
        Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap());
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 删除文档,按查询api
     * @throws IOException
     */
    @Test
    public void deleteBySearch () throws IOException {
        String method = "POST";
        String endpoint = "/delete-index/_delete_by_query";
        HttpEntity entity = new NStringEntity(
                "{\n" +
                        "  \"query\": { \n" +
                        "    \"match\": {\n" +
                        "      \"message\": \"begin\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}", ContentType.APPLICATION_JSON);
        Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 查询匹配,message为条件
     * @throws IOException
     */
    @Test
    public void search () throws IOException {
        String method = "POST";
        String endpoint = "/delete-index/_search/";
        HttpEntity entity = new NStringEntity(
                "{\n" +
                        "  \"query\": { \n" +
                        "    \"match\": {\n" +
                        "      \"message\": \"trying begin Elasticsearch\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}", ContentType.APPLICATION_JSON);
        Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

}

 

转载于:https://www.cnblogs.com/heqiyoujing/p/11148220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值