原创

ElasticSearch6.X版本Java Api中文详解(三)之Get Api、Delete API 、Delete By Query API解析

Get 
Delete
Delete By Query
Get Api

get API允许根据它的id从索引中获得一个类型化的JSON文档,下面的例子从一个名为twitter的索引中获得一个JSON文档,在一个名为tweet的类型下,id值为1:

GetResponse response = client.prepareGet("twitter", "tweet", "1").get();
Delete Api

delete API允许一个人根据其id从特定的索引中删除一个类型化的JSON文档,下面的例子从一个名为twitter的索引中删除了JSON文档,在一个名为tweet的类型下,id值为1:

DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();
Delete By Query

查询API的delete允许根据查询的结果删除给定的一组文档:

BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
    .filter(QueryBuilders.matchQuery("gender", "male")) 
    .source("persons")                                  
    .get();                                             
long deleted = response.getDeleted();  
因为它可以是一个长时间运行的操作,如果您希望异步地完成它,您可以调用execute而不是get并提供一个侦听器:

复制代码
DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
    .filter(QueryBuilders.matchQuery("gender", "male"))     
    .source("persons")                                      
    .execute(new ActionListener<BulkByScrollResponse>() {   
        @Override
        public void onResponse(BulkByScrollResponse response) {
            long deleted = response.getDeleted();           
        }
        @Override
        public void onFailure(Exception e) {
            // Handle the exception
        }
    });
复制代码
正文到此结束
该篇文章的评论功能已被站长关闭
本文目录