You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 08:01:23