删除所有小于给定值的元素在Elasticsearch中。

huangapple go评论75阅读模式
英文:

delete all elements which is less than a value 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"
   }
}

我想使用 Spring Data Elasticsearch 中的 QueryBuilder 删除所有 ids 的列表,其 expirationDate 小于今天。

英文:

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

答案1

得分: 9

Well, [delete by query][1] is the way to go.

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

The [java client documentation][2] 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][3].

You can for example use [query derivation][4] :

> 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`][5] (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).


  [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
  [2]: https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.9/java-docs-delete-by-query.html#java-docs-delete-by-query
  [3]: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#new-features.3-2-0
  [4]: https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.core-concepts
  [5]: https://docs.spring.io/spring-data/elasticsearch/docs/current/api/index.html?org/springframework/data/elasticsearch/core/ElasticsearchTemplate.html
英文:

Well, delete by query is the way to go.

POST /{your_index_name}/_delete_by_query
{
  &quot;query&quot;: {
    &quot;range&quot;: {
      &quot;expirationDate&quot;: {
        &quot;lt&quot;: &quot;{your_timestamp}&quot;
      }
    }
  } 
}

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

BulkByScrollResponse response =
  new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
    .filter(QueryBuilders.matchQuery(&quot;gender&quot;, &quot;male&quot;)) 
    .source(&quot;persons&quot;)                                  
    .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&lt;User, Long&gt; {
  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).

huangapple
  • 本文由 发表于 2020年10月23日 02:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64488359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定