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

2020-06-12 16:19:31 浏览数 (2)

阅读文本大概需要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
}'

0 人点赞