Flexible Search in SAP Commerce [Hybris]
In this article, I have explained in details about Flexible Search in SAP Commerce Cloud [SAP Hybris] with some real time examples.
What is Flexible Search in SAP Hybris?
Flexible Search is a Hybris built-in query language based on SQL syntax and it enables us to search records from the database using item types.
- In flexible search queries we never use database table names directly, instead we use item types which are mapped to corresponding tables in the items.xml file.
- In flexible search query we must specify item type code and its attributes in curly braces: e.g. Select {code}, {name} from {Product}.
- Pre-parsing phase: In this phase, Hybris converts the flexible query into equivalent SQL query.
- Executing the SQL converted query: In this phase, the converted SQL query will be executed by Hybris.
|
Select * from
{Product} |
|
Select
{code}, {name[en]} from {Product} where {code} = ‘123456’ |
|
Select
{c.uid}, {c.name[en]} from {Customer as c} |
- In Hybris, when we execute any flexible search query, by default it retrieves all the records including its sub-types. For example, if we run below flexible search query: Select * from {Product} - It fetches all records from Product and its sub-type Variant Product.
- If we want to exclude sub types in the search result then we must specify exclamation(!) explicitly as show below:
|
Select * from
{Product!} |
- In Flexible Search Query we can specify JOIN to retrieve the records from more than one item types.
|
Select * from
{Country JOIN Region} |
|
Select
{c.isoCode} Country_Code, {c.name} Country_Name, {r.name} Region_Name from
{Country as c JOIN Region as r ON {r.country} = {c.pk}} where {c.isoCode} =
'US' |
|
Select
{c.code}, {c.name}, {p.code}, {p.name} from {Category as c JOIN
CategoryProductRelation as rel ON {rel.source}={c.pk} JOIN Product as p ON
{rel.target}={p.pk}} where {code} = '1360' |
|
Select * from
{Product} where {code} = ‘123456’ |
|
Select * from
{Employee} where {name} LIKE ‘%john%’ Order By {name} ASC |
|
Select * from
{Country as c Join Region as r on {region.coutry} = {c.pk}} where {c.isoCode}
= ‘IN’ and {r.name} LIKE %A% |
|
Select {p.pk}
from {Product as p} where {p.code} = ?code |
|
final
Map<String, Object> queryParams = new HashMap<String, Object>(); String fQuery
=”SELECT {p.pk} FROM {Product as p} WHERE {p.code} = ?code” queryParams.put("code",”123456”); final
SearchResult<ProductModel> searchResult =
flexibleSearchService.search(query, queryParams); List< ProductModel
> productList = searchResult.getResult(); |
Happy Learning😊
Ramesh V
Comments
Post a Comment