使用Java客户端Api时如何将_delete_by_query限制为单个类型

时间:2018-06-26 04:52:45

标签: elasticsearch

我想使用Java客户端从es删除一些文档,例如

curl -X POST "localhost:9200/twitter/tweet/_delete_by_query?conflicts=proceed" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'

但来自document Apis

BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
    .filter(QueryBuilders.matchQuery("gender", "male")) 
    .source("persons")                                  
    .get();                                             
long deleted = response.getDeleted();                   

看来您不能limit _delete_by_query to a single type

1 个答案:

答案 0 :(得分:0)

您总是可以这样:

QueryBuilder query = QueryBuilders.boolQuery()
    .filter(QueryBuilders.termQuery("_type", "tweet"))
    .filter(QueryBuilders.matchQuery("gender", "male"));

BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
    .filter(query) 
    .source("persons")                                  
    .get();                                             
long deleted = response.getDeleted();