7、使用Java Low Level REST Client操作elasticsearch

阅读文本大概需要3分钟。

1、        根据Field字段模糊匹配查询

public static void queryByField(RestClient client) {
    try{
        String method = "POST";
        Stringendpoint = "/book/it/_search";
        HttpEntityentity = new NStringEntity("{\n" +
                "  \"query\":{\n" +
                "    \"match\":{\n" +
                "      \"name\":\"三\"\n" +
                "    }\n" +
                "  }\n" +
                "}", ContentType.APPLICATION_JSON);
        Requestrequest = new Request(method, endpoint);
        request.setEntity(entity);
        Responseresponse = client.performRequest(request);
        System.out.println(EntityUtils.toString(response.getEntity()));
        // 返回结果
        // {"took":3,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.5753642,"hits":[{"_index":"book","_type":"novel","_id":"1","_score":0.5753642,"_source":{"count":10,"name":"三国演义","publishDate":1555825698934,"writer":"张飞"}}]}}
    }catch (Exception e) {
        e.printStackTrace();
    }finally {
        try{
        client.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:

 

{
    "took": 42,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.5753642,
        "hits": [{
            "_index": "book",
            "_type": "it",
            "_id": "1",
            "_score": 0.5753642,
            "_source": {
                "count": 10,
                "name": "三国演义",
                "publishDate": 1561471991012,
                "writer": "张飞"
            }
        }]
    }
}

2、        更新索引

public static void updateDocument(RestClient client) {

    try {

        // doc_as_upsert :使用doc_as_upsert可以在文档不存在的时候,把doc中的内容插入到文档中

        String method = "POST";

        String endpoint = "/book/it/1/_update";

        HttpEntity entity = new NStringEntity("{\n" +

                "  \"doc\": {\n" +

                "    \"name\":\"三国演义修改哈哈哈\"\n" +

                "  }\n" +

                "}", ContentType.APPLICATION_JSON);

        Request request = new Request(method, endpoint);

        request.setEntity(entity);

        Response response = client.performRequest(request);

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

    }catch (Exception e) {

        e.printStackTrace();

    }finally {

        try{

            client.close();

        }catch (Exception e) {

            e.printStackTrace();

        }

    }

}

运行结果

{
    "_index": "book",
    "_type": "it",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

3、        删除索引

public static void deleteDocument(RestClient client) {

    try {

        String method = "DELETE";

        String endpoint = "/book/it/1";

        HttpEntity entity = new NStringEntity("", ContentType.APPLICATION_JSON);

        Request request = new Request(method, endpoint);

        request.setEntity(entity);

        Response response = client.performRequest(request);

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

    }catch (Exception e) {

        e.printStackTrace();

    }finally {

        try{

            client.close();

        }catch (Exception e) {

            e.printStackTrace();

        }

    }

}

运行结果

{
    "found": true,
    "_index": "book",
    "_type": "it",
    "_id": "1",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

根据条件删除

public static voiddeleteDocumentByCondition(RestClient client) {

    try {

        String method = "DELETE";

        String endpoint = "/book/it/_delete_by_query";

       /*

        {

            "query":{

                "term":{

                    "author":"test2"

                }

            }

        }

        */



       HttpEntity entity = new NStringEntity("{\n" +

                "  \"query\": {\n" +

                "    \"term\":\"三国演义修改哈哈哈\"\n"+

                "  }\n" +

                "}", ContentType.APPLICATION_JSON);

        Request request = new Request(method, endpoint);

        request.setEntity(entity);

        Response response = client.performRequest(request);

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

    }catch (Exception e) {

        e.printStackTrace();

    }finally {

        try{

            client.close();

        }catch (Exception e) {

            e.printStackTrace();

        }

    }

}

备注:

使用doc_as_upsert可以在文档不存在的时候,把doc中的内容插入到文档中。

curl -XPOST 'localhost:9200/test/type1/1/_update'-d '{
    "doc" : {
        "name" : "new_name"
    },
    "doc_as_upsert" : true
}'

往期精彩

01 漫谈发版哪些事,好课程推荐

02 Linux的常用最危险的命令

03 精讲Spring Boot—入门+进阶+实例

04 优秀的Java程序员必须了解的GC哪些

05 互联网支付系统整体架构详解

关注我

每天进步一点点

很干!在看吗?☟

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BUG弄潮儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值