Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch 2.0: how to delete by query in Java

I am trying to upgrade to ES 2.0. I have downloaed ES 2.0 and installed it on my Windows machine.

In my pom.xml, I have the following:

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.0.0-rc1</version>
</dependency>

<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>delete-by-query</artifactId>
    <version>2.0.0-rc1</version>
</dependency>

In my Java code, I did delete by query in the following way when using ES 1.7.3:

    StringBuilder b = new StringBuilder("");
    b.append("{");
    b.append("  \"query\": {");  
    b.append("      \"term\": {");
    b.append("          \"category\": " + category_value );
    b.append("      }");
    b.append("  }");
    b.append("}");

    client = getClient(); 

    DeleteByQueryResponse response = client.prepareDeleteByQuery("myindex")
                .setTypes("mydocytype")
                .setSource(b.toString())
                .execute()
                .actionGet();

I am hoping to replace this:

    DeleteByQueryResponse response = client.prepareDeleteByQuery("myindex")
                .setTypes("mydocytype")
                .setSource(b.toString())
                .execute()
                .actionGet();

with ES 2.0 way. Googled but failed to find an example for it. The online API documentation seems too abstract to me. How can I do it?

Another question: Do I have to install delete-by-query plugin in Elasticsearch server?

Thanks for any pointer!

UPDATE

I followed Max's suggestion, and here is what I have now:

First, when create the client, make settings look like the following:

Settings settings = Settings.settingsBuilder()
                        .put("cluster.name", "mycluster")
                        .put("plugin.types", DeleteByQueryPlugin.class.getName())
                        .build();

Second, at the place doing delete-by-query:

    DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
    .setIndices("myindex")
    .setTypes("mydoctype")
    .setSource(b.toString())
    .execute()
    .actionGet();

I also installed delete by query plugin by running the following in the root directory of ES:

bin\plugin install delete-by-query

I get errors if I do not install this plugin.

After all these steps, ES related parts work just fine.

like image 511
curious1 Avatar asked Oct 30 '15 06:10

curious1


People also ask

How do I delete a specific record in Elasticsearch?

You use DELETE to remove a document from an index. You must specify the index name and document ID. You cannot send deletion requests directly to a data stream. To delete a document in a data stream, you must target the backing index containing the document.

How does delete work in Elasticsearch?

You can specify the query criteria in the request URI or the request body using the same syntax as the Search API. When you submit a delete by query request, Elasticsearch gets a snapshot of the data stream or index when it begins processing the request and deletes matching documents using internal versioning.

How do I delete a type in Elasticsearch?

You can use _delete_by_query path to delete type. Save this answer.

What is the use of Elasticsearch in Java?

Elasticsearch is a real-time distributed and open source full-text search and analytics engine. It is used in Single Page Application (SPA) projects. Elasticsearch is an open source developed in Java and used by many big organizations around the world. It is licensed under the Apache license version 2.0.


1 Answers

plugin.types have been deprecated in ES 2.1.0 (source). So the accepted solution will result in a NullPointerException.

The solution is to use the addPlugin method:

Client client = TransportClient.builder().settings(settings())
                .addPlugin(DeleteByQueryPlugin.class)
                .build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host",9300));
like image 113
Ümit Avatar answered Sep 30 '22 23:09

Ümit