ES5.1 JavaAPI deletebyqueryRequestBuilder

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?

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;
...

DeleteByQueryAction.newRequestBuilder(client)
1 Like

DeleteByQueryResponse rsp =
new DeleteByQueryRequestBuilder(Client, DeleteByQueryAction.INSTANCE)
.setIndices("Indice")
.setTypes("Type")
.setQuery(QueryBuilders.termQuery("rowid", rowid))
.execute()
.actionGet();

I changed it to deleteByqueryAction.

BulkIndexByScrollResponse = DeleteByQueryAction.INSTANCE.newRequestBuilder(Client)
.source("Indice")
.filter(QueryBuilders.termQuery("rowid", rowid))
.execute()
.actionGet();

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

You can add a filter on type field with https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-type-query.html

thanks!

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

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?

Yes.

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)
.source("Indice")
.filter(QueryBuilders.termQuery("rowid", rowid))
.filter(QueryBuilders.typeQuery("Type")
.execute()
.actionGet();

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

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.

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.

QueryBuilders.boolQuery().filter(clause1).filter(clause2)

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