Spring Data Elasticsearch中UpdateQuery的withRefreshPolicy不生效问题
问题描述
使用Spring Data Elasticsearch 5.0.3的ElasticsearchOperations执行更新操作,代码如下:
esOperations.update( UpdateQuery.builder(old.id.toString()) .withDocument(Document.from(mapOf("browsed" to item.browsed))) .withRefreshPolicy( RefreshPolicy.IMMEDIATE ) .build(), IndexCoordinates.of("t_telegram_message") )
实际生成的请求为:
POST /t_telegram_message/_update/6150?refresh=false
预期请求参数应为refresh=true。
解决方法
1. 检查全局配置的覆盖情况
查看项目配置文件(如application.yml或application.properties),确认是否存在如下全局刷新策略配置:
spring.data.elasticsearch.refresh-policy=false
如果有该配置,它会覆盖请求级别的RefreshPolicy设置,直接删除即可恢复请求级别的参数生效。
2. 直接传入字符串形式的refresh参数
Spring Data Elasticsearch的UpdateQuery.Builder支持直接传入字符串类型的刷新策略值。由于RefreshPolicy枚举中没有对应true的枚举项,可直接使用字符串"true"指定:
esOperations.update( UpdateQuery.builder(old.id.toString()) .withDocument(Document.from(mapOf("browsed" to item.browsed))) .withRefreshPolicy("true") // 直接指定字符串参数 .build(), IndexCoordinates.of("t_telegram_message") )
修改后生成的请求会带上refresh=true参数。
3. 确认版本兼容性
Spring Data Elasticsearch 5.0.3对应Elasticsearch 8.6.x版本,若你的Elasticsearch服务版本与该版本不匹配,可能出现参数解析异常。确保两者版本兼容。
内容的提问来源于stack exchange,提问作者SageJustus
相关产品推荐
相关产品推荐

