请提供一些代码实现这样正确吗? public void update(UUID id,long currentDate){UpdateQuery updateQuery = UpdateQuery.builder(id.toString()).withRouting("expirationDate = currentDate").建造();elasticsearchTemplate.bulkUpdate(List.of(updateQuery),IndexCoordinates.of("index")));}} 解决方案如果您使用的是Elasticsearch 7.xx,我将假定您使用的是Spring boot 2.3.x随附的Spring Data Elasticsearch版本4.0.x.因为它是支持Elasticsearch 7.xx的版本.此Spring Data Elasticsearch版本中有很多更改.通过查询更新文档就是其中之一.与之前我们自动连接ElasticsearchTemplate的方式不同,现在我们必须改为使用ElasticsearchRestTemplate和RestHighLevelClient.在您的情况下,如果您想使用RestHighLevelClient通过查询更新文档.假设您以秒为单位将 expirationDate 存储为数字映射类型,则您要求的代码应如下所示. 公共类ElasticsearchService {@Autowired私有ElasticsearchRestTemplate elasticsearchRestTemplate;@Autowired私有RestHighLevelClient highLevelClient;公共无效updateExpireDateDemo()引发IOException {字符串indexName ="test";日期currentDate =新的Date();长秒=(长)(currentDate.getTime()/1000);UpdateByQueryRequest request = new UpdateByQueryRequest(indexName);request.setQuery(new RangeQueryBuilder("expirationDate").lte(seconds));脚本updateScript =新脚本(ScriptType.INLINE,无痛","ctx._source.expirationDate ="+秒+;",Collections.emptyMap());request.setScript(updateScript);highLevelClient.updateByQuery(request,RequestOptions.DEFAULT);}} 我不太了解为什么您确实需要使用bulkUpdate,但是如果是这样的话.您必须先查询需要从Elasticsearch更新的记录,才能首先获取每个文档的ID.然后,您可以使用UpdateQuery列表进行更新.因此您的代码将如下所示. @Service公共类ElasticsearchService {@Autowired私有ElasticsearchRestTemplate elasticsearchRestTemplate;公共无效updateExpireDateByBulkDemo()引发IOException {字符串indexName ="test";日期currentDate =新的Date();长秒=(长)(currentDate.getTime()/1000);列表< UpdateQuery>updateList =新的ArrayList();RangeQueryBuilder expireQuery = new RangeQueryBuilder("expirationDate").lte(seconds);NativeSearchQuery查询=新的NativeSearchQueryBuilder().withQuery(expireQuery).build();SearchHits< Data>searchResult = elasticsearchRestTemplate.search(query,Data.class,IndexCoordinates.of(indexName));for(SearchHit< Data> hit):searchResult.getSearchHits()){字符串elasticsearchDocumentId = hit.getId();updateList.add(UpdateQuery.builder(elasticsearchDocumentId).withScript("ctx._source.expirationDate =" +秒+;").build());}如果(updateList.size()> 0){elasticsearchRestTemplate.bulkUpdate(updateList,IndexCoordinates.of(indexName));}}} 但是,这只会更新搜索结果的第一页.如果您需要更新与查询匹配的每条记录,则必须在oder中使用 searchScroll 方法来获取每个文档ID.I have this database data as below (ES 7.xx) version { "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" }}how can i update the entity which its expirationDate smaller than current Time? to be the current time for example:the id 00021 is expired because its expiration date is smaller than today then it should updated to current time.something like void updateExpiredEntity(List<ids> ids,Long currentTime) suing void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index);Please provide me some code implementationis it correct like this? public void update(UUID id,Long currentDate) { UpdateQuery updateQuery = UpdateQuery.builder(id.toString()).withRouting("expirationDate=currentDate") .build(); elasticsearchTemplate.bulkUpdate(List.of(updateQuery), IndexCoordinates.of("index")); }} 解决方案 If you are using Elasticsearch 7.xx, I will assume that you have use Spring Data Elasticsearch version 4.0.x that comes with Spring boot 2.3.x. Since it's the version that support Elasticsearch 7.xx.There're many things that have change in this Spring Data Elasticsearch version. Update document by query is one of them. Unlike before that we autowired ElasticsearchTemplate, we now have to use ElasticsearchRestTemplate and RestHighLevelClient instead.In your case if you might want to use RestHighLevelClient to update document by query. Assume that you stored expirationDate as number mapping type in seconds unit then the code that you have asked for should look like this.public class ElasticsearchService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Autowired private RestHighLevelClient highLevelClient; public void updateExpireDateDemo() throws IOException { String indexName = "test"; Date currentDate = new Date(); Long seconds = (Long) (currentDate.getTime() / 1000); UpdateByQueryRequest request = new UpdateByQueryRequest(indexName); request.setQuery(new RangeQueryBuilder("expirationDate").lte(seconds)); Script updateScript = new Script( ScriptType.INLINE, "painless", "ctx._source.expirationDate=" + seconds + ";", Collections.emptyMap()); request.setScript(updateScript); highLevelClient.updateByQuery(request, RequestOptions.DEFAULT); }}I'm not quite get why you really need to use the bulkUpdate but if that's the case then. You have to query the record that need to be update from Elasticsearch to get and id of each document first. Then you can update with list of UpdateQuery. So your code will look like this.@Servicepublic class ElasticsearchService { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; public void updateExpireDateByBulkDemo() throws IOException { String indexName = "test"; Date currentDate = new Date(); Long seconds = (Long) (currentDate.getTime() / 1000); List<UpdateQuery> updateList = new ArrayList(); RangeQueryBuilder expireQuery = new RangeQueryBuilder("expirationDate").lte(seconds); NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(expireQuery).build(); SearchHits<Data> searchResult = elasticsearchRestTemplate.search(query, Data.class, IndexCoordinates.of(indexName)); for (SearchHit<Data> hit : searchResult.getSearchHits()) { String elasticsearchDocumentId = hit.getId(); updateList.add(UpdateQuery.builder(elasticsearchDocumentId).withScript("ctx._source.expirationDate=" + seconds + ";").build()); } if (updateList.size() > 0) { elasticsearchRestTemplate.bulkUpdate(updateList, IndexCoordinates.of(indexName)); } }}However, this only update first page of the search result. If you require to update every record matched your query then you have to use searchScroll method in oder to get every document id instead. 这篇关于批量更新Elasticsearch实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-04 19:21