Flexible search
- 28th Nov 2016
- 16
- 157474
- executing flexible search queries using java API in hybris Flexible search in Hybris flexible search query in hybris flexible search query syntax and overview in hybris How flexible search works in hybris how to add run time parameters in flexible search query in hybris how to search data from database in hybris
Lets understand some basic concepts about flexible search in Hybris with few examples
It’s a hybris built-in query language based on SQL syntax.
It enables us to search the recordsfrom the database using item types.
In flexible search queries, we never use database table names.
We always use item types which will be mapped to a corresponding tables by Hybris.
Flexible search query execution has 2 phases
1) Pre-parsing phase
In this phase, Hybris converts the flexible search query into SQL query.
2) Executing the SQL converted query
In this phase,converted SQL query will be executed by Hybris.
Note:
In Flexible search query, we should specify the types and attributes within curly braces.
Examples :
- Select * from {Product}
Select * from {Product}
This query retrieves all the columns and rows of a Product item type.
The item type specified in the curly braces is the exact item type code defined in the items.xml.
This should have been mapped to specific table using deployment tag in items.xml
for more details on how tables and item types are related, click here and here
- SELECT {name[de]}, {name[en]} FROM {Product}
SELECT {name[de]}, {name[en]} FROM {Product}
This query retrieves name of a Product in German and English locale
- SELECT {p.code} FROM {Product AS p} ORDER BY {p.code}
SELECT {p.code} FROM {Product AS p} ORDER BY {p.code}
This query retrieves Product code from Product type using alias with the name “p” for Product type.
We can specify the join between 2 types as below
- SELECT * FROM {Product JOIN Category}
SELECT * FROM {Product JOIN Category}
This query makes the Join between Product and Category item type.
Note:
We can make different types of Joins like Left outer join,Right outer join and Inner join.
Sub Types
When we search any type in flexible search, by default its subtypes will also be retrieved in the result.
Example:
- Select * from {Product}
Select * from {Product}
This will retrieve the instances of Product and VariantProduct.
Since VariantProduct is a sub type of Product, it retrieves its sub types as well.
If we want to exclude the subtypes in the result, we must use exclamation mark (!) while specifying type as below
- Select * from {Product!}
Select * from {Product!}
In this example, we have used ! for product type and hence it retrieves only Product instances but not its variants.
Using conditions
- SELECT * FROM {Product} WHERE {code} LIKE '%001%'
SELECT * FROM {Product} WHERE {code} LIKE '%001%'
Retrieves all the products whose code has 001 in it.
- SELECT * FROM {Product} WHERE {code} LIKE '%001%' AND {code} LIKE '%m%'
SELECT * FROM {Product} WHERE {code} LIKE '%001%' AND {code} LIKE '%m%'
Retrieves all the products whose code has 001 and m in it.
- SELECT * FROM {Product} WHERE {code} NOT LIKE '%001%'
SELECT * FROM {Product} WHERE {code} NOT LIKE '%001%'
Retrieves all the products whose code does not contain 001 in it.
- SELECT {code},{pk} FROM {Product} ORDER BY {code} DESC
SELECT {code},{pk} FROM {Product} ORDER BY {code} DESC
Retrieves the result and Sorts the search results based on the codecolumn in the database in descending order
Runtime Parameters
We can specify run time parameters in flexible search query using placeholder(?) prefix to a specific column.
- SELECT {p:pk} FROM {Product AS p} WHERE {p:code} LIKE ?code
SELECT {p:pk} FROM {Product AS p} WHERE {p:code} LIKE ?code
Here we have specified ?code which means run time parameter is code whose value will be added atrun time using query params HashMap.
Executing flexible search queries using API
- final Map<String, Object> params = new HashMap<String, Object>();
- String query =”SELECT {p:pk} FROM {Product AS p} WHERE {p:code} LIKE ?code”
- params.put("code",”001”);
- final SearchResult<ProductModel> searchResult = flexibleSearchService.search(query, params);
final Map<String, Object> params = new HashMap<String, Object>(); String query =”SELECT {p:pk} FROM {Product AS p} WHERE {p:code} LIKE ?code” params.put("code",”001”); final SearchResult<ProductModel> searchResult = flexibleSearchService.search(query, params);
We are passing run time parameters in the HashMap and passing them to the search method.
Note:
We can pass any number of runtime parameters in the query param using HashMap.
flexibleSearchService object has to be injected by Spring
will you give me a little bit of clarification, about itemType name and database table name?
How to write a script to add where condition with language? e.g. SELECT {name[de]}, {name[en]} FROM {Product} where {name[en]} like “%water%” ? is it work? I write it, but can’t work.
I am getting following error:
Caused by: java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000
when query gets executed using getFlexibleSearchService().search(query);
Thanks.
How does the outer join work in Flexible Search? “:o”
Couldn’t really understand from the docs
Why don’t we directly access the database ?
What thing actually maps the database attributes and the item attributes?
Hi KB ,
Could you please help me to create “SuperCategory from the join of product and category model using Flexible Search Query “.
I tried lots of time but not getting exact data as expected .
Hi KB,
Can you give information about how to write one to many and many to many flexible search query?
Why it has: p.code in some query Or P:code in run time query ? What is difference ?
p.code and P:code are equivalent. Both of those will work.
Hi,
Why hybris has provided flexible search when we are having simple sql or other query languages? What is benefit of using flexible search over sql?
Please explain in details. Thanks in advance.
Hi KB,
Can you say how to write subqueries through flexible search?
I got it.
hi,
i want to search a data which field name remarks.
result should be lowercase(db.remarks)=lowercase(remarksVariable)
please help
Hi KB,
Many thanks for this beautiful article.
Could you please help me to understand how to use flexisearch on N:M
relation.
Thanks and Regards,
Vivek Gupta
Thank you so much boss for giving such a useful tutorials for hybris….
You are welcome 🙂