FlexibleSearchQuery response no data

2019-08-17 03:03发布

问题:

I am trying to get some data using FlexibleSearchQuery but it is responding empty result. If I set this query hac>Console>Flexible Search I can get the data I am looking for. Here is how I am writing query in java file.

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);
}

Here is how I call search

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

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

If I type it in console

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'}})

I get

PK
8796093579265
8796101804033
8796100165633
8796098428929
8796093153281
8796102361089
8796097052673
8796093808641
8796093972481
8796096823297

What am I doing wrong? Is there any difference console query and string query?

回答1:

I think that the problem in your case is related to Restrections, by definition : Restrictions are rules obeyed by FlexibleSearch which allow to limit search results depending on which type is searched and which user is currently logged in

so try to Disable search restrictions before calling your flexible seach

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

In other hand , when you use hac>Console>Flexible Search i suppose that you log as admin , so all restrictions are disabled by default in Hybris Flexible search

You can check all actives restrctions in HMC, by searchig the type : SearchRestriction , look the followinf screenshot

see : https://help.hybris.com/6.3.0/hcd/8c428f8286691014970ceee87aa01605.html



回答2:

Another way to bypass restriction is to use sessionService to execute

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

I find this solution more elegant. :-)



标签: hybris