A 20-minute crash course

Learn SAP Commerce FlexibleSearch

For developers, functional consultants, and support folks who need to query Hybris data in hAC or Java code. Every example on this page is a real, runnable query.

This is a learning simulator. It runs real SQL against a small practice dataset so every example and exercise executes for real — but actual results in your project depend on your type system, restrictions, and catalog versions.
📦 Meet the data — click to see every table this page queries

Catalog (2)

electronicsProductCatalog
apparelProductCatalog

CatalogVersion (4)

electronics — Staged
electronics — Online
apparel — Staged
apparel — Online

Category (5)

cameras / laptops / phones
accessories
tshirts

Product (12) + VariantProduct (3)

CAM-100, CAM-200, CAM-900 (staged)
LAP-300, LAP-400 (no price), LAP-1000
PHN-500, PHN-600 (unapproved), PHN-1100
ACC-700, ACC-800, TSHIRT-BASE
+ 3 Classic Tee variants (S/M/L)

PriceRow (14)

One per product, INR or USD
LAP-400 deliberately has none

Warehouse (2) + StockLevel (28)

warehouse_mumbai, warehouse_hyderabad
CAM-200 deliberately has no stock rows
LAP-1000 is outOfStock in Mumbai only

Customer (8)

Jane Doe, Raj Kumar, Amelia Wong…
Noah Schmidt deliberately has no orders

Order (10) + OrderEntry (15)

Statuses: CREATED, COMPLETED, CANCELLED
Dates spread 2024-05-02 → 2024-06-27

ArticleApprovalStatus (3)

approved, check, unapproved

CategoryProductRelation (13)

source = Category.pk, target = Product.pk
ACC-700 is deliberately in 2 categories

How the tables connect

Every join you'll write on this page is one of these lines. An arrow always points from a foreign-key column to the pk it references — the same direction you write in a JOIN's ON clause, e.g. {oe:product} = {p:pk}.

1:N — a direct foreign keyM:N — two FKs through a join tableCatalogArticleApprovalStatusWarehouseCustomerCatalogVersionOrderCategoryProductOrderEntryM:N: Order ⇄ ProductCPRM:N join tableVariantProductextends ProductPriceRowStockLevelcatalog (1:N)catalogVersion (1:N)catalogVersion (1:N)approvalStatus (1:N)baseProduct (1:N)sourcetargetproduct (1:N)productCode ≠ pk! (1:N)warehouse (1:N)user (1:N)orderproduct
Teal = one-to-many, a direct foreign key — the normal case, 10 of the 13 arrows here. Amber = many-to-many, which SAP Commerce always builds from two one-to-many FKs meeting in a small join/line-item table: CPR (CategoryProductRelation) makes Category ⇄ Product many-to-many, and OrderEntry makes Order ⇄ Product many-to-many the same way. One more thing to watch: StockLevel.productCode references Product.code, not Product.pk — not every link in SAP Commerce goes through pk.

One-to-many: one customer, many orders

A solid arrow on the diagram means "many of these can point at one of those." Here, one Customer row can match many Order rows — so a customer with several orders shows up once per order below. Jane Doe appears three times; Raj Kumar twice; everyone else with an order appears once. Noah Schmidt doesn't appear at all — he has zero orders, and a plain JOIN only keeps rows that match on both sides (section 9 covers LEFT JOIN, which would keep him with NULLs instead).

or press Ctrl/Cmd + Enter
10 rows
namecodestatus
Amelia WongORD-1004COMPLETED
Carlos SilvaORD-1005COMPLETED
Jane DoeORD-1001COMPLETED
Jane DoeORD-1003CANCELLED
Jane DoeORD-1009COMPLETED
Layla HassanORD-1010CREATED
Pallavi PaswanORD-1007COMPLETED
Raj KumarORD-1002COMPLETED
Raj KumarORD-1006CREATED
Tom BeckerORD-1008COMPLETED

Many-to-many: one product, several categories — and back

A join table lets both sides be "many" at once — neither Category nor Product stores the other's pk directly; CategoryProductRelation holds one row per pairing instead. ACC-700 (Camera Strap) is deliberately tagged under two categories, so it proves the relationship both ways:

Product → its categories (expect 2 rows for ACC-700)

or press Ctrl/Cmd + Enter
2 rows
code
accessories
cameras

Category → its products (cameras now includes ACC-700 too)

or press Ctrl/Cmd + Enter
4 rows
code
CAM-100
CAM-200
CAM-900
ACC-700
01

What FlexibleSearch is

FlexibleSearch is SAP Commerce's own query language — SQL-like, but written against your type system (Product, Order, Customer…) instead of raw database tables. You run it in hAC's console, through FlexibleSearchService in Java code, inside ImpEx scripts, and in CMS restriction rules.

02

Basic syntax

Types and attributes both go in curly braces — {Type} for what you're querying, {attribute} for the columns you want back.

or press Ctrl/Cmd + Enter
15 rows
pkcode
8796093000001CAM-100
8796093000002CAM-200
8796093000003LAP-300
8796093000004LAP-400
8796093000005PHN-500
8796093000006PHN-600
8796093000007ACC-700
8796093000008ACC-800
8796093000009CAM-900
8796093000010LAP-1000
8796093000011PHN-1100
8796093000012TSHIRT-BASE
8796094000001TSHIRT-BASE-S-RED
8796094000002TSHIRT-BASE-M-BLUE
8796094000003TSHIRT-BASE-L-GREEN

Try it: add {name[en]} to the SELECT list and run again.

03

Why you always select {pk}

FlexibleSearchService doesn't hand you raw rows — it takes the pk from each result row and loads the real model object for it. Forget {pk} and setResultClassList has nothing to build your models from.

What actually happens under the hood

  1. Your FlexibleSearch query runs against the database and comes back as plain rows of values — numbers and strings, nothing Java-shaped yet.
  2. setResultClassList(List.of(ProductModel.class)) tells the service: "for every row, build me one of these."
  3. To build a ProductModel, the service needs to know which item it's building. It gets that from exactly one place: the pk value in the row. Every other selected column — code, name[en], whatever — is ignored for this purpose.
  4. Using that pk, it loads the full item from the system — all its attributes, localized values, relations — not just the columns you happened to SELECT. Your SELECT list only controlled which rows matched; it never controlled what the resulting model contains.
  5. If a row has no pk column, step 3 has nothing to key off of. FlexibleSearchService throws an exception immediately — it doesn't silently return a half-built object.

So {code} and {name[en]} in the query below aren't "the fields on the returned object" — they're just extra columns in the raw row, unused once pk has done its job of loading the real ProductModel. Try removing {pk} from the query and note what the error-shaped intuition would be: a real call with setResultClassList set would throw at that point.

Without {pk}

Nothing to load a model from. Fine only if you never call setResultClassList with a model class.

SELECT {code} FROM {Product} → setResultClassList(ProductModel.class) → throws: no pk in result

With {pk}

Each row's pk loads a full ProductModel — code/name in the SELECT just narrowed which rows matched.

SELECT {pk}, {code} FROM {Product} → setResultClassList(ProductModel.class) → List<ProductModel> back, fully populated

Try it

or press Ctrl/Cmd + Enter
12 rows
pkcodename_en
8796093000001CAM-100Compact Camera X100
8796093000002CAM-200DSLR Camera X200
8796093000003LAP-300UltraBook 14
8796093000004LAP-400Gaming Laptop 17
8796093000005PHN-500Smartphone S5
8796093000006PHN-600Smartphone S6 Pro
8796093000007ACC-700Camera Strap
8796093000008ACC-800Laptop Sleeve 14
8796093000009CAM-900Mirrorless Camera X900
8796093000010LAP-1000Business Laptop 13
8796093000011PHN-1100Smartphone S11 Lite
8796093000012TSHIRT-BASEClassic Tee
04

WHERE

Filter rows the same way you would in SQL — =, LIKE, IN, AND / OR.

or press Ctrl/Cmd + Enter
6 rows
code
CAM-100
CAM-200
PHN-500
PHN-600
CAM-900
PHN-1100

IN

or press Ctrl/Cmd + Enter
3 rows
code
CAM-100
LAP-300
PHN-500
05

Parameters

Use ?name instead of hardcoding values — it's safer (no string-building SQL by hand) and lets the platform cache and reuse the query plan across calls with different values.

or press Ctrl/Cmd + Enter
1 row
codename_en
CAM-100Compact Camera X100

Try it: change the ?code value in the Parameters panel and rerun.

06

Localized attributes

Attributes that store a value per language take a language code in brackets: {name[en]} vs {name[de]}.

or press Ctrl/Cmd + Enter
5 rows
codename_enname_de
camerasCamerasKameras
laptopsLaptopsLaptops
phonesPhonesTelefone
accessoriesAccessoriesZubehoer
tshirtsT-ShirtsT-Shirts
07

ORDER BY and limiting results

Sort with ORDER BY same as SQL. For limits, hAC has a separate Max count field below the editor — in Java it's query.setCount(n) (and setStart(n) to page). Try it below.

or press Ctrl/Cmd + Enter
5 rows
codeonlineDate
TSHIRT-BASE2023-06-01
PHN-11002023-05-15
LAP-10002023-05-01
PHN-6002023-04-18
PHN-5002023-04-05
08

Catalog versions

This is the #1 real-world mistake: forget to filter by catalog version, and you'll get both Staged and Online rows back. "CAM-900" only exists in Staged — watch it appear below.

Without a version filter (the mistake)

or press Ctrl/Cmd + Enter
3 rows
code
CAM-100
CAM-200
CAM-900

Filtered to Online (the fix)

or press Ctrl/Cmd + Enter
2 rows
code
CAM-100
CAM-200
09

Joins

Joins live inside the FROM braces, with aliases so you can qualify attributes as {alias:attribute}.

JOIN

or press Ctrl/Cmd + Enter
10 rows
codename
ORD-1001Jane Doe
ORD-1002Raj Kumar
ORD-1003Jane Doe
ORD-1004Amelia Wong
ORD-1005Carlos Silva
ORD-1006Raj Kumar
ORD-1007Pallavi Paswan
ORD-1008Tom Becker
ORD-1009Jane Doe
ORD-1010Layla Hassan

LEFT JOIN — products with no price row

or press Ctrl/Cmd + Enter
1 row
code
LAP-400
10

Relations

Many-to-many links like category ↔ product live in their own relation type, with source and target columns — join through it like any other type.

or press Ctrl/Cmd + Enter
4 rows
code
CAM-100
CAM-200
CAM-900
ACC-700
11

Enums

Enum attributes store a pk, not a string — comparing {approvalStatus} to 'approved' directly will quietly match nothing. Look the code up with a subquery instead.

or press Ctrl/Cmd + Enter
1 row
code
PHN-600
12

Subtypes

{Product} includes subtypes like VariantProduct. Add ! — {Product!} — to get exactly that type and nothing else.

{Product} — includes the 3 Classic Tee variants

or press Ctrl/Cmd + Enter
15 rows
pkcode
8796093000001CAM-100
8796093000002CAM-200
8796093000003LAP-300
8796093000004LAP-400
8796093000005PHN-500
8796093000006PHN-600
8796093000007ACC-700
8796093000008ACC-800
8796093000009CAM-900
8796093000010LAP-1000
8796093000011PHN-1100
8796093000012TSHIRT-BASE
8796094000001TSHIRT-BASE-S-RED
8796094000002TSHIRT-BASE-M-BLUE
8796094000003TSHIRT-BASE-L-GREEN

{Product!} — base products only

or press Ctrl/Cmd + Enter
12 rows
pkcode
8796093000001CAM-100
8796093000002CAM-200
8796093000003LAP-300
8796093000004LAP-400
8796093000005PHN-500
8796093000006PHN-600
8796093000007ACC-700
8796093000008ACC-800
8796093000009CAM-900
8796093000010LAP-1000
8796093000011PHN-1100
8796093000012TSHIRT-BASE
13

Subqueries

Wrap a nested query in double braces: {{ SELECT ... }}. Handy for "none of these" checks.

Products with no stock anywhere

or press Ctrl/Cmd + Enter
1 row
code
CAM-200

Customers who never ordered

or press Ctrl/Cmd + Enter
1 row
name
Noah Schmidt
14

Aggregates

COUNT, SUM, GROUP BY, and HAVING work exactly like SQL — group by an attribute, then filter the groups.

or press Ctrl/Cmd + Enter
3 rows
nametotal
Jane Doe219493
Carlos Silva92997
Raj Kumar64999
15

Using it in Java

Same query text, run from a service bean. Not runnable here — just the shape you'll actually type.

JAVA
final String fsq =
    "SELECT {pk} FROM {Product} WHERE {code} = ?code";

final FlexibleSearchQuery query = new FlexibleSearchQuery(fsq);
query.addQueryParameter("code", "CAM-100");
query.setResultClassList(List.of(ProductModel.class));
query.setCount(50);

final SearchResult<ProductModel> result =
    flexibleSearchService.search(query);
final List<ProductModel> products = result.getResult();
16

Restrictions & session context

In a real storefront, session context (current user, catalog version, language) silently narrows every query through configured restrictions — so the same FlexibleSearch query can return different rows for different users. hAC runs as an admin session with restrictions off by default, which is exactly why a query that "works in hAC" can behave differently from the storefront.

Common mistakes

  • Forgetting the catalog version filterYou'll get Staged and Online rows mixed together — see section 8.
  • Hardcoding values instead of parametersUse ?name, not string-concatenated literals.
  • Comparing an enum to a plain stringEnum attributes store a pk — look the code up with a subquery, see section 11.
  • Using {'{Product}'} when you meant {'{Product!}'}Non-exact types silently include subtypes like VariantProduct.
  • Selecting columns instead of {'{pk}'}If you need model objects back, pk has to be in the SELECT list.
  • Running heavy, unlimited queries in production hACAlways set a Max count — hAC runs as admin with no query timeout by default.

Cheat sheet

SyntaxWhat it doesExample
{Type}A type, including its subtypesFROM {Product}
{Type!}Exactly that type, no subtypesFROM {Product!}
{Type AS a}Type with an aliasFROM {Product AS p}
{a:attr}Attribute qualified by alias{p:code}
{attr[lang]}A localized attribute{name[en]}
JOIN / LEFT JOINInside the FROM braces, with ON{A AS a JOIN B AS b ON ...}
{{ }}A subqueryWHERE {pk} IN {{ SELECT ... }}
?nameA named parameterWHERE {code} = ?code
Max countRow limit (hAC field / Java setCount)query.setCount(50)

Coming from SQL

In SQLIn FlexibleSearch
SELECT col FROM tableSELECT {attribute} FROM {Type}
WHERE col = 'x'WHERE {attribute} = ?param
INNER JOIN t2 ON t1.x = t2.yJOIN inside the FROM braces
a bare column namealways wrapped: {attribute}
LIMIT nMax count field / setCount(n)

FlexibleSearch Console

A blank scratchpad, styled like hAC — write anything, set a max count, and run it against the same practice data.

or press Ctrl/Cmd + Enter

Type browser

Catalog
pkid
CatalogVersion
pkcatalogversionactive
Category
pkcodename[en]name[de]catalogVersion
ArticleApprovalStatus
pkcode
Product
pkcodename[en]name[de]description[en]catalogVersionapprovalStatusuniteanonlineDate
VariantProductextends Product
pkcodename[en]name[de]description[en]catalogVersionapprovalStatusuniteanonlineDatebaseProduct
CategoryProductRelation
sourcetarget
PriceRow
pkproductpricecurrencyminqtd
Warehouse
pkcode
StockLevel
pkproductCodewarehouseavailableinStockStatus
Customer
pkuidnamecustomerIDcreationtime
Order
pkcodeuserdatetotalPricecurrencystatus
OrderEntry
pkorderproductquantitybasePricetotalPrice

Practice

10 questions against the same dataset. Write a query, hit Run to see what comes back, then Check answer when you think it's right.

0/10 solved
1

All product codes and English names in the electronics Online catalog.

2

Products that are not approved.

3

Products in the "cameras" category.

4

Products with no price row.

5

Products out of stock in warehouse_mumbai.

6

Customers who have never placed an order.

7

Total COMPLETED order value per customer, highest first.

8

Only base products, excluding variants.

9

Top 3 best-selling products by total quantity ordered.

10

Orders placed in the last 30 days of the dataset (on or after 2024-05-31), using a ?fromDate parameter.

Everything on this page runs client-side against an in-memory SQLite database — nothing you type here is sent anywhere.

🗳️ Vote for the next SAP Commerce guide

  • FlexibleSearch joins explained with real examples42 votes
  • ImpEx: insert, update, remove with macros and translators0 votes
  • Creating a custom OCC endpoint step by step0 votes
Vote or suggest a topic
What should we write next? Vote