前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >7、使用Java Low Level REST Client操作elasticsearch

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

作者头像
BUG弄潮儿
发布2020-06-12 16:19:31
8430
发布2020-06-12 16:19:31
举报
文章被收录于专栏:JAVA乐园JAVA乐园

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

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

代码语言:javascript
复制
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();
        }
    }
}

运行结果:

代码语言:javascript
复制
{
    "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、 更新索引

代码语言:javascript
复制
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();

        }

    }

}

运行结果

代码语言:javascript
复制
{
    "_index": "book",
    "_type": "it",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

3、 删除索引

代码语言:javascript
复制
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();

        }

    }

}

运行结果

代码语言:javascript
复制
{
    "found": true,
    "_index": "book",
    "_type": "it",
    "_id": "1",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

根据条件删除

代码语言:javascript
复制
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中的内容插入到文档中。

代码语言:javascript
复制
curl -XPOST 'localhost:9200/test/type1/1/_update'-d '{
    "doc" : {
        "name" : "new_name"
    },
    "doc_as_upsert" : true
}'
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档