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).


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>)?


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:


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 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).


import de.hybris.platform.servicelayer.user.UserService;
private UserService userService;

2. Enabling or Disabling Search Restrictions:


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

// Enable search restrictions
// some query goes here

Creating Restrictions

  1. Creating Restrictions via the SAP Hybris(Commerce) API
final SearchRestrictionModel search = modelService.create(SearchRestrictionModel.class);
final SearchRestrictionModel searchRestriction = modelService.create(SearchRestrictionModel.class);
searchRestriction.setQuery("{" + PrincipalGroupModel.UID + "} NOT IN ( 'Jacksons' )");

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.