前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3、Elasticsearch删除和更新Index

3、Elasticsearch删除和更新Index

作者头像
BUG弄潮儿
发布2020-06-12 15:56:18
8830
发布2020-06-12 15:56:18
举报
文章被收录于专栏:JAVA乐园JAVA乐园

1、 使用Delete Index API删除Document

代码语言:javascript
复制
public static DeleteResponse getDeleteResponse(TransportClient client,
                                              String index,
                                              String type,
                                               Stringid) {
    DeleteResponse response =client.prepareDelete(index, type, id).get();
    return response;
}

测试

代码语言:javascript
复制
//先删除
 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、 根据条件删除

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

}

测试

代码语言:javascript
复制
//先删除

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

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

更多参考

代码语言:javascript
复制
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发送

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

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

}

测试:

代码语言:javascript
复制
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());
本文参与?腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-02,如有侵权请联系?cloudcommunity@tencent.com 删除

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

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

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com