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

REST API调用FlexibleSearch报session.currentCountry解析错误排查

问题描述
  • 本地环境直接调用FlexibleSearch查询代码可正常返回预期结果
  • 通过Controller暴露的REST API触发相同逻辑时抛出异常:could not translate value expression 'session.currentCountry'
  • 手写的FlexibleSearch语句中未显式使用任何session、session country相关逻辑,相同业务流程仅在远程实例触发REST接口时报错,本地运行无异常

涉及的FlexibleSearch查询语句

select {rr.pk} from {returnrequest as rr 
join order as r on {r.pk} = {rr.order} 
join orderstatus as os on {r.status}={os.pk} 
join basestore as bs on {bs.pk}={r.store} 
join returntype as rt on {rr.returntype} = {rt.pk} 
join paymentmode as pm on {pm.pk}={r.paymentmode} 
join returnstatus as rs on {rs.PK}={rr.status}} 
where {os.code} NOT in ('PENDING_PAYMENT','ON_VALIDATION', 'CREATED', 'IN_PROGRESS', 'ORDER_SPLIT','CANCELLED') 
and {rr.creationtime} >= ?returnCreationDateFrom 
and {rr.creationtime} < ?returnCreationDateTo 
and {bs.uid} in (?market) 
and {rt.code} in (?returnType) 
and {pm.code} = 'SplittedPaymentMode' 
and {rr.totalRefund} != 0.0 
and {rs.code}!='RECEIVED' 
group by {rr.pk}

查询执行代码

FlexibleSearchQuery query = new FlexibleSearchQuery(flexiQuery.toString());
query.addQueryParameter("market", market);
query.addQueryParameter("returnType", returnType);
query.addQueryParameter("returnCreationDateTo", DateUtils.addDays(returnCreationDateTo, 1));
query.addQueryParameter("returnCreationDateFrom", returnCreationDateFrom);
SearchResult<ReturnRequestModel> results = search(query);

错误日志

ERROR PaymentWsController de.hybris.platform.servicelayer.search.exceptions.FlexibleSearchException: could not translate value expression 'session.currentCountry'
19:37:31.834 [hybrisHTTP6] ERROR de.hybris.platform.jalo.flexiblesearch.FlexibleSearch - Flexible search error occured...
问题根因

报错和手写的查询语句无关,是Hybris平台自动追加的搜索限制规则触发的:
远程实例上配置了SearchRestriction(搜索限制),规则里引用了session.currentCountry会话值做数据权限过滤,所有关联到对应类型的FlexibleSearch执行时,平台会自动把这段限制条件拼接到最终生成的SQL里。
本地直接调用逻辑时,会话是完整初始化的,session中已经注入currentCountry属性,规则可以正常解析;但REST API走的是无状态接口请求,对应的会话没有提前设置currentCountry属性,平台解析自动拼接的限制表达式时就会抛出该错误。

解决方案
  • 方案1:执行查询前手动给当前会话注入currentCountry属性,适配搜索限制规则的取值要求
// 获取当前请求对应的会话
JaloSession session = JaloSession.getCurrentSession();
// 按业务实际场景获取对应国家模型,示例为取中国区配置
CountryModel currentCountry = commonI18NService.getCountry("CN");
session.getSessionContext().setAttribute("currentCountry", currentCountry);
// 后续执行原有FlexibleSearch查询逻辑即可
  • 方案2:如果该查询为后台统计类逻辑,不需要受前台数据权限规则约束,可在创建查询对象时临时关闭搜索限制,注意该操作会绕过所有配置的数据权限规则,仅在内部可信逻辑中使用
FlexibleSearchQuery query = new FlexibleSearchQuery(flexiQuery.toString());
// 临时关闭搜索限制自动拼接
query.setDisableSearchRestrictions(true);
// 原有参数设置逻辑
query.addQueryParameter("market", market);
query.addQueryParameter("returnType", returnType);
query.addQueryParameter("returnCreationDateTo", DateUtils.addDays(returnCreationDateTo, 1));
query.addQueryParameter("returnCreationDateFrom", returnCreationDateFrom);
SearchResult<ReturnRequestModel> results = search(query);
  • 方案3:登录远程实例后台,排查SearchRestriction配置列表,找到引用了session.currentCountry的限制规则,给规则增加执行范围判断,避免在WS接口的无状态会话场景下触发该规则。

内容的提问来源于stack exchange,提问作者KUNAL HIRANI

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 14:33:30