GenericSearch
While SAP FlexibleSearch offers a powerful search API to developers, some of them may prefer the GenericSearch API that is similar to Hibernate Criteria Queries. Hibernate is a collection of related projects, enabling developers to utilize POJO-style domain models in their applications in ways extending well beyond Object and Relational Mapping.
Introduction
Both the FlexibleSearchService and the GenericSearch API allow developers to construct and execute queries against the SAP Commerce database, by focusing on SAP Commerce items rather than raw SQL. However, the way in which the queries are constructed is based on two completely different and complimentary approaches.
A FlexibleSearchService query is constructed by placing a FlexibleSearch query statement in a String, for example:
""SELECT {" + CategoryModel.PK + "} FROM {" + CategoryModel._TYPECODE + "} WHERE {" CategoryModel.ID "}=?id";
The ?id placeholder in the query is the place for a real condition parameter that is replaced during query execution.
In contrast to this, a query, for the GenericSearchService is constructed by combining instances of GenericField and GenericCondition to form GenericQuery. GenericQuery is a Java-based object describing the search criteria, an example of which is shown below. Users of Hibernate ORM can see a close parallel between this approach and the Hibernate's Criteria Queries.
final String categoryID = "Foo";
final GenericCondition idCondition = GenericCondition.equals(
new GenericSearchField(CategoryModel._TYPECODE, CategoryModel.ID ), categoryID);
final GenericConditionList gl = GenericCondition.createConditionList(idCondition);
final GenericQuery query = new GenericQuery(CategoryModel._TYPECODE, gl);
To execute the FlexibleSearch, use the FlexibleSearchService:
Collection result = flexibleSearchService.<CategoryModel> search("SELECT
{" + CategoryModel.PK + "} FROM {" + CategoryModel._TYPECODE + "}
WHERE {" CategoryModel.ID "}").getResult();
Likewise, to execute the GenericSearch, use the GenericSearchService:
Collection result = genericSearchService.search(query).getResult();
This document describes the latter approach, of performing GenericSearch calls by constructing GenericQuery instances. See FlexibleSearch for a detailed discussion on the former. Additionally, see the hybris Platform Search Mechanisms document for an overview of all search mechanisms available in SAP Commerce.
GenericSearch
The GenericSearch is a search framework that allows you to easily search on items stored within the Platform. Its functionality may be a bit limited, but on the other hand, it is easy to understand and to handle. Although it technically uses the FlexibleSearch, it encapsulates the FlexibleSearch syntax and usage. The functionality of the GenericSearch includes:
-
Searching of items as well as raw data fields
-
Unlimited number of conditions
-
Inner joins and outer joins between item types possible
-
Unlimited number of order by clauses
-
Subselects.
The GenericSearch is the tool of choice for a typical Find me all products sort of query that the real FlexibleSearchService would be an overkill to work with. If you do not have extensive experience with the Platform, it is recommended to get familiar with the GenericSearch at first and use it as a ladder to work yourself upwards to the FlexibleSearch.
Before an actual GenericSearch statement is executed, it is run through a syntax checker. In other words, an incorrect statement is never executed. On top of that, you may assign names to GenericSearch statements so you may save them in the database.
To run a GenericSearch statement, set up a query and then have the current session run that query. Technically, you create a GenericQuery object and add fields to it, that contain the parts of your query that you want. An example for that:
// create a query to find products GenericQuery query = new GenericQuery(ProductModel._TYPECODE); // run the search genericSearchService.search(query).getResult();
First, it creates a query that looks for products. Then it runs that query. Since the query is not limited in any way, it will find all products within the platform. Now, refine the query:
// create a query to find products final GenericQuery query = new GenericQuery(ProductModel._TYPECODE); // create a new field as a container for the search results // the field searches in ProductModel.NAME // roughly translated into SQL terms: SELECT FROM ProductModel.Name final GenericSearchField nameField = new GenericSearchField(ProductModel._TYPECODE, ProductModel.NAME); // add a search condition to the field created above // corresponds about to the SQL statement: // WHERE (nameField) IS LIKE 'display' final GenericCondition condition = GenericCondition.createConditionForValueComparison(nameField, Operator.LIKE, "display"); // Add that search condition to query query.addCondition(condition); // order by name - ascendingly query.addOrderBy(new GenericSearchOrderBy(nameField, true)); // run the search genericSearchService.search(query).getResult();
The listing above searches for all products with names containing the string display and presents them in an ascendant order by their name. The next listing extends that even more:
// create a query to find products final GenericQuery query = new GenericQuery(ProductModel._TYPECODE); // create a new field as a container for the search results // the field searches in ProductModel.NAME // roughly translated into SQL terms: SELECT FROM ProductModel.Name final GenericSearchField nameField = new GenericSearchField(ProductModel._TYPECODE, ProductModel.NAME); // add a search condition to the field created above // corresponds about to the SQL statement: // where (nameField) IS LIKE 'display' final GenericCondition condition = GenericCondition.createConditionForValueComparison(nameField, Operator.LIKE, "display"); // Add that search condition to query query.addCondition(condition); // order by name - ascendingly query.addOrderBy(new GenericSearchOrderBy(nameField, true)); //create a join with all medias within the platform GenericCondition joinCondition = GenericCondition.createJoinCondition(productMediaField, mediaField); // add the join to the previous search so that only entries that // match both searches are returned query.addInnerJoin(joinCondition); // run the search genericSearchService.search(query).getResult();
This listing returns all products with names containing the string display that have a media assigned to them. If you need to, you can also assign a parameter to the search. The following listing shows how to do it:
// create a query to find products
final GenericQuery query = new GenericQuery(ProductModel._TYPECODE);
// create a new field as a container for the search results
// the field searches in ProductModel.NAME
// roughly translated into SQL terms: SELECT FROM ProductModel.Name
final GenericCondition condition = GenericCondition.createConditionForValueComparison(nameField, Operator.LIKE, "display", "myNameQualifier"); // (1)
// Add that search condition to query
query.addCondition(condition);
// order by name - ascendingly
query.addOrderBy(new GenericSearchOrderBy(nameField, true));
//create a join with all medias within the Platform
final GenericCondition joinCondition = GenericCondition.createJoinCondition(productMediaField, mediaField);
// add the join to the previous search so that only entries that match both searches are returned
query.addInnerJoin(joinCondition);
// run the search
genericSearchService.search(query).getResult();
//reset the value
query.getCondition().setResettableValue("myNameQualifier", "floppy"); // (2)
// find products with "floppy" in a name
genericSearchService.search(query).getResult();
The main difference is marked by (1) - it is the name of a parameter that you may hand over, for example in the URL. In the (2) this parameter is set to a new value, in this case String floppy. The search string to look for is then no longer display, but floppy. All other search parameters remain the same.
It is also possible to search for raw data instead of Item instances. To search for a code of a product, instead of for the Product itself, you should to do the following:
// the same code as above // get only a code of products final GenericSelectField codeField = GenericSelectField(ProductModel._TYPECODE, ProductModel.CODE, String.class); query.addSelectField(codeField); // find codes of products with "display" in their name genericSearchService.search(query).getResult();
GenericSearch in Action
The most helpful resource for learning and seeing the GenericSearch in action is to take a look at the JUnit test for it in. The code sample below contains a commented example taken from GenericSearchTest.java illustrating a simple GenericSearch:
GenericSearchTest.java
public void testGenericConditions() throws Exception{
//Create a GenericCondition
final GenericSearchField codeField = new GenericSearchField(ProductModel._TYPECODE, ProductModel.CODE);
GenericCondition condition = GenericCondition.createConditionForValueComparison(codeField, Operator.EQUAL, product1.getCode());
// Add this to a new GenericConditionList
final GenericConditionList conditionList = GenericCondition.createConditionList(condition);
// Create another GenericCondition and append it to this GenericConditionList
final GenericSearchField nameField = new GenericSearchField(ProductModel._TYPECODE, ProductModel.NAME);
conditionList.addToConditionList(GenericCondition.createConditionForValueComparison(nameField, Operator.STARTS_WITH,
"pRoduCt", /* upper */true));
// Create a GenericQuery
GenericQuery query = new GenericQuery(ProductModel._TYPECODE);
// Add the condition list to the GenericQuery
query.addCondition(conditionList);
// Also add two GenericSelectField to the GenericQuery
final GenericSelectField codeSelectField = new GenericSelectField(ProductModel._TYPECODE, ProductModel.CODE, String.class);
final GenericSelectField nameSelectField = new GenericSelectField(ProductModel._TYPECODE, ProductModel.NAME, String.class);
query.addSelectField(codeSelectField);
query.addSelectField(nameSelectField);
// Run the query
Collection result = genericSearchService.search(query, new StandardSearchContext(ctxDe)).getResult();
...
}
GenericSearchQuery as Container for GenericQuery
GenericSearchQuery is a container object that allows to configure some additional search criteria and other options, like a user, for which the query is executed or pagination. The base AbstractQuery class keeps common functionality for both GenericSearchQuery and FlexibleSearchQuery. One of the GenericSearchQuery constructors provides a possibility to pass GenericQuery object as follows:
final GenericCondition idCondition = GenericCondition.equals(new GenericSearchField(CatalogModel._TYPECODE, CatalogModel.ID), TEST_CATALOG_ID);
final GenericConditionList gl = GenericCondition.createConditionList(idCondition);
final GenericQuery query = new GenericQuery(CatalogModel._TYPECODE, gl);
final GenericSearchQuery gsq = new GenericSearchQuery(query);
gsq.setCount(5);
gsq.setUser(userService.getUserForUID("ahertz"));
gsq.setCatalogVersions(catalogVersionService.getCatalogVersion("clothes", "Online"));
final SearchResult<CatalogModel> searchResult = genericSearchService.search(gsq);
Behind the Scenes
Behind the scenes, the GenericQuery is ultimately converted into a FlexibleSearchQuery by the toFlexibleSearch method and than executed as FlexibleSearch query:
public String toFlexibleSearch(final Map valueMap){
final StringBuilder sb = new StringBuilder();
toFlexibleSearch(sb, null, valueMap);
return sb.toString();
}
GenericSearch in UML
The UML diagram below illustrates the relationship between the main GenericSearch objects.
