英文:
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
{
"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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论