Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all elements which is less than a value in Elasticsearch

I have the following saved json data in Elasticsearch:

   {
   "id":"1234",
   "expirationDate":"17343234234",
   "paths":"http:localhost:9090",
   "work":"software dev",
   "family":{
      "baba":"jams",
      "mother":"ela"
   }
},
{
   "id":"00021",
   "expirationDate":"0123234",
   "paths":"http:localhost:8080",
   "work":"software engi",
   "family":{
      "baba":"stev",
      "mother":"hela"
   }
}

i want to delete all list of ids which its expirationDate are smaller than today using QueryBuilder in springdata Elasticsearch

like image 492
Catalina Avatar asked Oct 22 '20 18:10

Catalina


1 Answers

Well, delete by query is the way to go.

POST /{your_index_name}/_delete_by_query
{
  "query": {
    "range": {
      "expirationDate": {
        "lt": "{your_timestamp}"
      }
    }
  } 
}

The java client documentation indicates you can build a request this way:

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

This is marked as supported by Spring-data-elasticsearch since version 3.2.

You can for example use query derivation :

In addition to query methods, query derivation for both count and delete queries is available.

In the Appendix C, you can see that IsLessThan is a query derivation keyword, which means something along these lines ought to be supported out of the box : 

interface YourRepository extends CrudRepository<User, Long> {
  long deleteByExpirationDateIsLessThan(long timestamp);
}

By using query derivation, you are letting spring do the implementation (fingers crossed that it will do "the right thing").

But you can also use a ElasticsearchRestTemplate#delete (if you are using the older ElasticsearchTemplate, this works the same).

This allows you to pass in any spring-data query (Native, String, or criteria).

like image 187
GPI Avatar answered Oct 08 '22 10:10

GPI