ES5.1 JavaAPI deletebyqueryRequestBuilder

(염경훈) #1

Elasticsearch version: 5.1.1

OS version: centos 7

Description of the problem including expected versus actual behavior:
ES 5.1 version does not support delete-by-query plugin.
so i need to change the javaAPI code.

The following code is from the previous 2.3 version.

new DeleteByQueryRequestBuilder(ElasticConnector.getInstance().getJavaClient(), DeleteByQueryAction.INSTANCE) .setIndices(GLOBAL_ID) .setTypes(MessageService.DEVICE) .setQuery(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("user_id", user_id)) .should(QueryBuilders.termQuery("device_id", device_id) .mustNot(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("user_id", user_id)) .must(QueryBuilders.termQuery("device_id", device_id)) )).execute().actionGet();t indent

This code has a problem with "DeleteByQueryRequestBuilder cannot be resolved to a type"
How do I change?

(Nik Everett) #2

In 5.1 delete by query was moved into the reindex module so it is shipped by default with Elasticsearch. You should be able to use it over the java API if you are using the PreBuiltTransportClient described here. This ought to work:

import org.elasticsearch.index.reindex.DeleteByQueryAction;


(염경훈) #3

DeleteByQueryResponse rsp =
new DeleteByQueryRequestBuilder(Client, DeleteByQueryAction.INSTANCE)
.setQuery(QueryBuilders.termQuery("rowid", rowid))

I changed it to deleteByqueryAction.

BulkIndexByScrollResponse = DeleteByQueryAction.INSTANCE.newRequestBuilder(Client)
.filter(QueryBuilders.termQuery("rowid", rowid))

.setIndices("Indice") is changed to .source("Indice"), and .setQuery() is changed to .filter()
Is that right?
And how do I change the .setType()?

(David Pilato) #4

You can add a filter on type field with

(염경훈) #5


Is setType used only for search?
Can not use delete?

(David Pilato) #6

You can use this type query within a bool query as a filter clause.
Add your QueryBuilders.termQuery("rowid", rowid) also as another filter clause.

Then add this bool query to .filter()

I'm curious about what you are doing though. Are you removing only one single document with the delete by query API?

(염경훈) #7


I want to delete documents that are executed by queryDSL
under specific index, type, and routing conditions.

and i add type condition, as below.

BulkIndexByScrollResponse = DeleteByQueryAction.INSTANCE.newRequestBuilder(Client)
.filter(QueryBuilders.termQuery("rowid", rowid))

.setRouting ("_ id") is still not resolved ...

(David Pilato) #8

I meant using a bool query which has 2 filters.

Something like:

"bool" : {
  "filter": [
    { "term" : { "rowid" : "rowid" } },
    { "type" : { "value" : "Type" } }

Then add this boolQuery with filter(boolQuery) to your delete by query operation.

(염경훈) #9

Thank you very much for your interest.
I did it the way you told me, but it does not work.
There is no filter(boolQuery), and there are no type-related functions in the BoolQueryBuilder.

(David Pilato) #10


(system) #11

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.