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

Elasticsearch删除和更新也都是写操作。但是Elasticsearch中的文档是不可变的,因此不能被删除或者改动以展示其变更。那么该如何删除和更新文档呢?

磁盘上的每个段都有一个相应的.del文件。当删除请求发送后,文档并没有真的被删除,而是在.del文件中被标记为删除。该文档依然能匹配查询,但是会在结果中被过滤掉。当段合并时,在.del文件中被标记为删除的文档将不会被写入新段。

接下来我们看更新是如何工作的。在新的文档被创建时,Elasticsearch会为该文档指定一个版本号。当执行更新时,旧版本的文档在.del文件中被标记为删除,新版本的文档被索引到一个新段。旧版本的文档依然能匹配查询,但是会在结果中被过滤掉。

物理删除索引:当索引数据不断增长时,对应的segment也会不断的增多,查询性能可能就会下降。因此Elasticsearch会触发segment合并的线程,把很多小的segment合并成更大的segment,然后删除小的segment,当这些标记为删除的segment不会被复制到新的索引段中。

1、删除索引Index

同步删除

public static void syncDeleteRequest(RestHighLevelClient client){
    try{
        DeleteRequest request = new DeleteRequest("it",//索引
                "_doc",//类型
                "2");//文档ID

        //同步执行
        DeleteResponsedeleteResponse = client.delete(request, RequestOptions.DEFAULT);
        //DeleteResponse
        //返回的DeleteResponse允许检索有关执行操作的信息,如下所示:
        Stringindex = deleteResponse.getIndex();
        Stringtype = deleteResponse.getType();
        Stringid = deleteResponse.getId();
        long version= deleteResponse.getVersion();
        System.out.println("index = "+ index);
        System.out.println("type = "+ type);
        System.out.println("id = "+ id);
        System.out.println("version = "+ version);
    }catch (Exception e){
        e.printStackTrace();
    }
}

异步删除:

public static voidasyncDeleteRequest(RestHighLevelClient client){


    DeleteRequest request = new DeleteRequest("it",//索引
            "_doc",//类型
            "2");//文档ID

    //异步执行
    //DeleteResponse  的典型监听器如下所示:
    //异步方法不会阻塞并立即返回。

    ActionListener<DeleteResponse > listener = new ActionListener<DeleteResponse >() {


        public void onResponse(DeleteResponse  getResponse) {

            //执行成功时调用。Response以参数方式提供

            //Delete Response

            //返回的DeleteResponse允许检索有关执行操作的信息,如下所示:

            String index = getResponse.getIndex();

            String type = getResponse.getType();

            String id = getResponse.getId();

            long version = getResponse.getVersion();

            System.out.println("index = "+ index);

            System.out.println("type = "+ type);

            System.out.println("id = "+ id);

            System.out.println("version = "+ version);

        }



        public void onFailure(Exception e) {

            //在失败的情况下调用。引发的异常以参数方式提供

        }

    };

    //异步执行获取索引请求需要将DeleteRequest  实例和ActionListener实例传递给异步方法:

    client.deleteAsync(request, RequestOptions.DEFAULT, listener);

}

2、       更新索引Index,更新Index有多种方式

Json字符串更新方式:

public static voidupdateJsonString( RestHighLevelClient client ) throws Exception{

    //只更新部分

    //更新部分文档时,更新的部分文档将与现有文档合并。

    //方式1:使用字符串形式

    UpdateRequest request = new UpdateRequest("it",   //索引

            "_doc",     // mapping type

            "3");

    String jsonString = "{" +

            "\"updated\":\"2017-01-01\"," +

            "\"reason\":\"daily update\"" +

            "}";

    request.doc(jsonString, XContentType.JSON);

    //同步执行

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

    //Update Response

    //返回的UpdateResponse允许检索有关执行操作的信息,如下所示:
    String index = updateResponse.getIndex();
    String type = updateResponse.getType();
    String id = updateResponse.getId();
    long version = updateResponse.getVersion();
    System.out.println("index = "+index);
    System.out.println("type = "+type);
    System.out.println("id = "+id);
    System.out.println("version = "+version);

}

Map更新方式:

public static voidupdateMap(RestHighLevelClient client)throws  Exception {



    //方式2:使用Map形式,会被自动转为json格式

    Map<String, Object> jsonMap = new HashMap<String, Object>();

    jsonMap.put("updated", new Date());

    jsonMap.put("reason", "daily update");

    UpdateRequest request = new UpdateRequest("it",   //索引

            "_doc",     // mapping type

            "3")

            .doc(jsonMap);

    //同步执行

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

    //Update Response

    //返回的UpdateResponse允许检索有关执行操作的信息,如下所示:

    String index = updateResponse.getIndex();

    String type = updateResponse.getType();

    String id = updateResponse.getId();

    long version = updateResponse.getVersion();

    System.out.println("index = "+index);

    System.out.println("type = "+type);

    System.out.println("id = "+id);

    System.out.println("version = "+version);

}

XContentBuilder更新方式:

public static voidupdateXContentBuilder(RestHighLevelClient client) throws  Exception {

    //方式3:使用XContentBuilder形式

    XContentBuilder builder = XContentFactory.jsonBuilder();

    builder.startObject();

    {

        builder.field("updated", new Date());

        builder.field("reason", "daily update");

    }

    builder.endObject();

    UpdateRequest request = new UpdateRequest("it",   //索引

            "_doc",     // mapping type

            "3")

            .doc(builder);

    //同步执行

    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

    //Update Response

    //返回的UpdateResponse允许检索有关执行操作的信息,如下所示:

    String index = updateResponse.getIndex();

    String type = updateResponse.getType();

    String id = updateResponse.getId();

    long version = updateResponse.getVersion();

    System.out.println("index = "+index);

    System.out.println("type = "+type);

    System.out.println("id = "+id);

    System.out.println("version = "+version);

}

Key-Value更新方式:

public static voidupdateKeyVal(RestHighLevelClient client) throws  Exception {

        //方式4:使用Object key-pairs形式

        UpdateRequest request = new UpdateRequest("it",   //索引

                "_doc",     // mapping type

                "3")

                .doc("updated", new Date(), "reason", "daily update");

        //同步执行

        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);

        //Update Response

        //返回的UpdateResponse允许检索有关执行操作的信息,如下所示:

        String index = updateResponse.getIndex();

        String type = updateResponse.getType();

        String id = updateResponse.getId();

        long version = updateResponse.getVersion();

        System.out.println("index = "+index);

        System.out.println("type = "+type);

        System.out.println("id = "+id);

        System.out.println("version = "+version);



//        //异步执行

//        //DeleteResponse  的典型监听器如下所示:

//        //异步方法不会阻塞并立即返回。

//        ActionListener<UpdateResponse > listener = new ActionListener<UpdateResponse >() {

//

//            public void onResponse(UpdateResponse  updateResponse) {

//                //执行成功时调用。Response以参数方式提供

//                String index = updateResponse.getIndex();

//                String type = updateResponse.getType();

//                String id = updateResponse.getId();

//                long version = updateResponse.getVersion();

//                System.out.println("index = "+index);

//                System.out.println("type = "+type);

//                System.out.println("id = "+id);

//                System.out.println("version = "+version);

//            }

//

//            public void onFailure(Exception e) {

//                //在失败的情况下调用。引发的异常以参数方式提供

//            }

//        };

//        //异步执行获取索引请求需要将UpdateRequest  实例和ActionListener实例传递给异步方法:

//        client.updateAsync(request, RequestOptions.DEFAULT, listener);

    }

测试代码:

try {

    RestHighLevelClient client = getClient();

    updateJsonString(client);

    updateMap(client);

    updateXContentBuilder(client);

    updateKeyVal(client);

    client.close();

}catch (Exception e){

    e.printStackTrace();

}


10、使用Java High Level REST Client操作elasticsearch_java