0
点赞
收藏
分享

微信扫一扫

3、Elasticsearch删除和更新Index


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

1、        使用Delete Index API删除Document

 

public static DeleteResponse getDeleteResponse(TransportClient client,
                                              String index,
                                              String type,
                                               Stringid) {
    DeleteResponse response =client.prepareDelete(index, type, id).get();
    return response;
}

 

测试

//先删除
 DeleteResponse deleteResponse = IndexDelete.getDeleteResponse(client,"twitter2", "tweet2", "2");

 System.out.println(deleteResponse.getIndex());

//在查找

 GetResponse getResponse = IndexGet.getGetResponse(client,"twitter2", "tweet2", "2");

 String str = getResponse.getSourceAsString();

 System.out.println(str);

2、        根据条件删除

 

public static BulkByScrollResponse getBulkByScrollResponse(TransportClient client,

                                               String index,

                                               String fieldName,

                                               String fieldVal) {

    BulkByScrollResponse response =

            DeleteByQueryAction.INSTANCE.newRequestBuilder(client)

                    .filter(QueryBuilders.matchQuery(fieldName, fieldVal)) //查询条件

            .source(index) //index(索引名)

            .get(); //执行

    return response;

}

测试

//先删除

BulkByScrollResponse deleteResponse = IndexDelete.getBulkByScrollResponse(client,"fendo", "user", "kimchy");

System.out.println(deleteResponse.getDeleted());

更多参考

https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.2/java-docs-delete-by-query.html

 

3、        使用Update Index  API更新Document

方式一:创建UpdateRequest ,通过client发送

 

public static UpdateResponse getUpdateResponse1(TransportClient client,

                                              String index,

                                              String type,

                                              String id) {

    UpdateRequest updateRequest = new UpdateRequest();

    updateRequest.index(index);

    updateRequest.type(type);

    updateRequest.id(id);

    UpdateResponse response = null;

    try {

        updateRequest.doc(XContentFactory.jsonBuilder()

                .startObject()

                .field("gender", "male")

                .endObject());

        response = client.update(updateRequest).get();

    }catch (Exception e){

        e.printStackTrace();

    }

    return response;

}

方式二:使用prepareUpdate() 方法

public static UpdateResponse getUpdateResponse2(TransportClient client,

                                                String index,

                                                String type,

                                                String id) {

    UpdateResponse response = null;

    try {

        response =  client.prepareUpdate(index, type, id)

            .setDoc(XContentFactory.jsonBuilder() //合并到现有文档

                    .startObject()

                    .field("gender", "male")

                    .field("age", "100")

                    .endObject())

            .get();

}catch (Exception e){

    e.printStackTrace();

}

    return response;

}

测试:

UpdateResponse response1 =  IndexUpdate.getUpdateResponse1(client,  "twitter2", "tweet2", "2");

System.out.println(response1.getIndex());



response1 = IndexUpdate.getUpdateResponse1(client,"twitter2", "tweet2", "2");

System.out.println(response1.getIndex());


每天进步一点点

3、Elasticsearch删除和更新Index_es


举报

相关推荐

0 条评论