Flexible Search and Restrictions in SAP Hybris(Commerce)

Nuray Fahri
3 min readJun 13, 2020

Flexible Search is a retrieval language build-in the SAP Hybris(Commerce). It enables searching for SAP Hybris(Commerce) types and items using an SQL-based syntax.

The execution of a FlexibleSearch statement takes place in two phases:

  1. Pre-parsing into an SQL-compliant statement: resolves the FlexibleSearch syntax into SQL Query.
  2. Running that statement on the database: SQL query is executed.

SAP Hybris(Commerce) executes FlexibleSearch queries in the context of a certain user account, using a session. As different user accounts have access to different items on the SAP Hybris(Commerce), the number of search results depends on the user account(Here restrictions play big role).

Syntrax

A FlexibleSearch query consists of:

  • The mandatory <selects> parameter for the SELECT clause.
  • The mandatory <types> parameter for the FROM clause.
  • An optional <conditions> field for the WHERE clause.
  • An optional ORDER BY clause.

The basic syntax of a FlexibleSearch query looks like this:

SELECT <selects> FROM <types> (WHERE <conditions>)? (ORDER BY <order>)?

Example:

Select * from {Customer} - this query returns all columns and rows of a Customer item type.

By default item’s subtypes will also be returned in the result. As B2BCustomer is subtype of Customer, B2BCustomer’s instances will be also returned.

Using exclamation mark (!) we can exclude the subtypes:

Select * from {Customer!}

Example with condition:

SELECT * FROM {Customer} WHERE {name} IS NOT NULL

Running flexible search queries using API

final Map<String, Object> params = new HashMap<String, Object>();String query =”SELECT * FROM {Customer AS c} WHERE {c:name} LIKE ?name”params.put("name",”Test”);
// Flexible search service injected by Spring
final SearchResult<ProductModel> searchResult = flexibleSearchService.search(query, params);

Note: To call a FlexibleSearch statement using the API use flexibleSearchService , which is always available through the Spring, and has to be properly injected

Restrictions

Restrictions allow to limit search results depending on which type is searched and which user is currently logged in.

SAP Hybris(Commerce) automatically WHERE clauses of all applicable FlexibleSearch statements and thereby restricts the number of search results of these statements due to these additional search conditions.

A restriction always applies to a specified type and it’s subtypes.

Disabling Restrictions

You are trying to get some data using FlexibleSearchQuery but it is responding empty result? You can try to disable restrictions:

  1. Assigning the Session to an Admin User: restrictions do not apply to admin users.

Assigning the session to an admin user has the side effect of granting the session access to every CatalogVersion in SAP Haybris(Commerce).

Example:

import de.hybris.platform.servicelayer.user.UserService;
...
@Autowired
private UserService userService;
...
userService.setCurrentUser(userService.getAdminUser())

2. Enabling or Disabling Search Restrictions:

Example:

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

Creating Restrictions

  1. Creating Restrictions via the SAP Hybris(Commerce) API
final SearchRestrictionModel search = modelService.create(SearchRestrictionModel.class);
search.setPrincipal(user);
final SearchRestrictionModel searchRestriction = modelService.create(SearchRestrictionModel.class);
searchRestriction.setActive(Boolean.TRUE);
searchRestriction.setGenerate(Boolean.TRUE);
searchRestriction.setCode("not_Jacksons");
searchRestriction.setPrincipal(user);
searchRestriction.setQuery("{" + PrincipalGroupModel.UID + "} NOT IN ( 'Jacksons' )");
searchRestriction.setRestrictedType(typeService.getComposedTypeForClass(PrincipalGroupModel.class));
modelService.save(searchRestriction);

2. Creating Restrictions via the ImpEx Extension

INSERT_UPDATESearchRestriction;code[unique=true];name[lang=en];query;principal(UID);restrictedType(code);active;generate;employee_restriction;Restrict employees visibility;EXISTS ({{ SELECT {pk} FROM {PrincipalGroupRelation} WHERE {source}={item:pk} AND {target} IN ( ?session.branch ) }} ) AND ( {item:active} = 1 OR EXISTS ( {{ select {ug:PK} from {UserGroup as ug} where {ug:PK} IN (?session.user.groups) and {ug:uid} = 'b2badmingroup' }} ));b2bgroup;B2BCustomer;true;true

Learn more about FlexibleSearch

Learn more about Restrictions

Make sure you give this post a clap and follow my blog if you find it helpful.

--

--