use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project elasticsearch-indexing-proxy by codelibs.
the class RequestUtils method createDeleteByQueryRequest.
public static DeleteByQueryRequestBuilder createDeleteByQueryRequest(final Client client, final StreamInput streamInput, final String index) throws IOException {
final DeleteByQueryRequestBuilder builder = client.prepareExecute(DeleteByQueryAction.INSTANCE);
final DeleteByQueryRequest request = builder.request();
request.readFrom(streamInput);
if (index != null) {
request.indices(index);
}
return builder;
}
use of org.elasticsearch.index.reindex.DeleteByQueryRequest in project gora by apache.
the class ElasticsearchStore method deleteByQuery.
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
try {
BulkByScrollResponse bulkResponse;
if (query.getFields() != null && query.getFields().length < elasticsearchMapping.getFields().size()) {
UpdateByQueryRequest updateRequest = new UpdateByQueryRequest(elasticsearchMapping.getIndexName());
QueryBuilder matchDocumentsWithinRange = QueryBuilders.rangeQuery("gora_id").from(query.getStartKey()).to(query.getEndKey());
updateRequest.setQuery(matchDocumentsWithinRange);
// Create a script for deleting fields
StringBuilder toDelete = new StringBuilder();
String[] fieldsToDelete = query.getFields();
for (String field : fieldsToDelete) {
String elasticsearchField = elasticsearchMapping.getFields().get(field).getName();
toDelete.append(String.format(Locale.getDefault(), "ctx._source.remove('%s');", elasticsearchField));
}
// toDelete.deleteCharAt(toDelete.length() - 1);
updateRequest.setScript(new Script(ScriptType.INLINE, "painless", toDelete.toString(), Collections.emptyMap()));
bulkResponse = client.updateByQuery(updateRequest, RequestOptions.DEFAULT);
return bulkResponse.getUpdated();
} else {
DeleteByQueryRequest deleteRequest = new DeleteByQueryRequest(elasticsearchMapping.getIndexName());
QueryBuilder matchDocumentsWithinRange = QueryBuilders.rangeQuery("gora_id").from(query.getStartKey()).to(query.getEndKey());
deleteRequest.setQuery(matchDocumentsWithinRange);
bulkResponse = client.deleteByQuery(deleteRequest, RequestOptions.DEFAULT);
return bulkResponse.getDeleted();
}
} catch (IOException ex) {
throw new GoraException(ex);
}
}
Aggregations