FlexibleSearchQuery响应没有数据

时间:2017-05-18 11:19:26

标签: hybris

我正在尝试使用FlexibleSearchQuery获取一些数据,但它正在响应空结果。如果我设置此查询hac>Console>Flexible Search,我可以获取我要查找的数据。 以下是我在java文件中编写查询的方法。

sb.append(" SELECT {p:pk} ");
        sb.append(" FROM {" + ProductModel._TYPECODE + " AS p} ");
        sb.append(" WHERE {p:" + ProductModel.PK + " } IN ({{");
        sb.append(" SELECT DISTINCT {pro:" + ProductModel.PK + "} ");
        sb.append(" FROM {CategoryProductRelation AS cp},{" + ProductModel._TYPECODE + "! AS pro},{" + CategoryModel._TYPECODE
                + " AS c}");
        sb.append(" WHERE {pro:" + ProductModel.PK + " } = {cp:target}");
        sb.append("  AND {c:" + CategoryModel.PK + " } = {cp:source}");
        sb.append("  AND {c:" + CategoryModel.CODE + " } !=?categoryCode    }})");



args.put("categoryCode", categoryCode);

if (args != null && !args.isEmpty())
{
            searchQuery.addQueryParameters(args);
}

以下是我如何调用搜索

final FlexibleSearchQuery searchQuery = new FlexibleSearchQuery(sb.toString());

searchResult.getResult().size()  >> retuns 0

如果我在控制台中输入

SELECT {p:pk} FROM { Product AS p} WHERE { p: pk } IN ({{ 

            SELECT DISTINCT {pro: PK } 

                  FROM 

            {CategoryProductRelation AS cp}, 
            {Product AS pro}, 
            {Category as c} 

            WHERE {pro: PK} = {cp:target}

            AND {c:PK} = {cp:source}
            AND {c:code} != '0101'}})

我得到了

PK
8796093579265
8796101804033
8796100165633
8796098428929
8796093153281
8796102361089
8796097052673
8796093808641
8796093972481
8796096823297

我做错了什么? 控制台查询和字符串查询有什么区别吗?

2 个答案:

答案 0 :(得分:1)

我认为您的案例中的问题与Restrections有关,顾名思义:限制是FlexibleSearch遵守的规则,允许根据搜索的类型和当前登录的用户来限制搜索结果

因此请在调用灵活搜索之前尝试禁用搜索限制

import de.hybris.platform.search.restriction.SearchRestrictionService;
...
// Disable search restrictions
searchRestrictionService.disableSearchRestrictions();
// some query goes here

// Enable search restrictions
searchRestrictionService.enableSearchRestrictions();
// some query goes here

另一方面,当您使用hac>Console>Flexible Search时,我认为您以管理员身份登录,因此在Hybris灵活搜索中默认禁用所有限制

您可以通过搜索类型:SearchRestriction检查HMC中的所有活动限制,查看以下屏幕截图

enter image description here

请参阅:https://help.hybris.com/6.3.0/hcd/8c428f8286691014970ceee87aa01605.html

答案 1 :(得分:0)

绕过限制的另一种方法是使用sessionService来执行

sessionService.executeInLocalView(new SessionExecutionBody() { @Override public void executeWithoutResult() { // do stuff } }, userService.getAdminUser());

我觉得这个解决方案更优雅。 : - )