标签:
基础知识
该FlexibleSearch是内置采用hybris Commerce Suite的一个强大的检索语言。它能采用hybris商务套件使用基于SQL语法搜索类型和物品。
一个FlexibleSearch语句的执行发生在两个阶段:
pre-parsing(预解析)到一个SQL-compliant语句中和运行这个语句在数据库上
在预解析阶段,FlexibleSearch框架解析FlexibleSearch语法到SQL-compliant里。下面的两个代码段表现出一个FlexibleSearch查询和这个FlexibleSearch语句查询的结果,这是在数据库上执行的语句:
FlexibleSearch查询语句:
elect {pk}, {code}, {name[de]} from {Product}在数据库上执行的SQL语句:
SELECT item_t0.PK , item_t0.Code , lp_t0.p_name FROM products item_t0 JOIN productslp lp_t0 ON item_t0.PK = lp_t0.ITEMPK AND lp_t0.LANGPK= 9013632135395968 WHERE (item_t0.TypePkString IN ( 23087380955301264 , 23087380955663520 , 23087380955662768 , 23087380955661760 , 23087385363574432 , 23087380955568768 , 23087380955206016 ) )
该FlexibleSearch采用hybris Commerce Suite的类型系统从实际的数据库表这样你就可以在类型相同级别运行查询。不同于常规的SQL语句,在一个FlexibleSearch查询中您不必指定明确的数据库表名。该FlexibleSearch框架解析类型和数据库表自动依赖关系并指定必要UNIONS和JOINS在哪里。类型系统和数据库表示之间的整个转换过程是自动进行的。要访问一个类型中FlexibleSearch查询,请用花括号{and},如类型代码:
SELECT * FROM {Product}
采用hybris Commerce Suite的在某一用户帐户的上下文中执行FlexibleSearch查询,使用一个会话。因为不同的用户帐户访问在采用hybris商务套件的不同项目,搜索结果的数量取决于用户的帐户。搜索结果的数目被按类型的访问权限定义(这些仅影响采用hybris管理控制台的搜索结果),限制,目录版本和类别。
更多的特权用户帐户是,在更多的搜索结果FlexibleSearch产量在用户帐户的上下文中。默认情况下,分配给会话的用户帐户是匿名的,所以任何FlexibleSearch查询返回的搜索结果相匹配的默认匿名帐户。
匿名不同用户帐户的情况下运行从FlexibleSearch查询,该会话需要被分配给不同的用户帐户,如:
import de.hybris.platform.servicelayer.user.UserService; ... // Injected by Spring userService.setCurrentUser(userService.getUserForUID("myUserAccount")); ...
语法概要
FlexibleSearch查询的基本语法如下:
SELECT <selects> FROM <types> ( WHERE <conditions> )? ( ORDER BY <order> )?一个FlexibleSearch查询包括:
强制性的<selects>参数对SELECT子句。
强制性的<type>参数为对FROM子句。
强制性的一个可选的<conditions>字段的WHERE子句。
一个可选的ORDER BY子句。
SQL Command / Keyword | Description / Comment | Code Example |
ORDER BY {alias:attribute} | Display results ordered by the value of attribute. | SELECT ... FROM ... ORDER BY... |
ASC | 排序结果以升序 (a...z, 0...9). | SELECT ... FROM ... ORDER BY ... ASC |
DESC | 排序结果以降序(z...a, 9...0). | SELECT ... FROM ... ORDER BY ... DESC |
DISTINCT | Eliminates double entries in the query result. | SELECT DISTINCT ... FROM ... |
OR | Performs a logical OR compare between two queries. | ... WHERE ... OR ... |
AND | Performs a logical AND compare between two queries. | ... WHERE ... AND ... |
IS [NOT] NULL | Returns the results that are [not] null | ... WHERE ... IS [NOT] NULL |
[NOT] IN | Returns the results that are [not] part of the following statement ... WHERE ... [NOT] IN ... | |
[NOT] EXISTS | Returns the results that are [not] matching a given subquery. | ...WHERE ... EXISTS ( {{ SELECT ... }} ) |
LIKE | Compares to a pattern. | ... WHERE ... LIKE ‘...‘ |
% | Wildcard matching any number of characters. | ... WHERE ... LIKE ‘%...‘||‘...%...‘||‘...%‘ |
_ | Wildcard matching a single character. | ... WHERE ... LIKE ‘...‘ || ‘......‘ ||‘..._‘ |
LEFT JOIN ON | Merges two tables into one. | ... LEFT JOIN ... ON ... = ... |
= | True if results are equal. | |
!=, <> | True if results are not equal. | |
< | True if result 1 is less than result 2. | |
<= | True if result 1 is equal to or less than result 2. | |
> | True if result 1 is greater than result 2. | |
>= | True if result 1 is equal to or greater than result 2. | |
CONCAT | 连接两个结果 - 在右边侧的例子将返回字符串结果。 | CONCAT ( ‘resul‘, ‘t‘ ) |
:o | 外连接参数用于包括与在ProductsLP表(=. 表包含本地化产品)以及缺少行匹配结果否则,例如查询将只返回产品 与ProductsLP表中现有行,因为它只会使用JOIN。 | SELECT {p:PK} FROM {Product AS p} WHERE {p:description[en]:o} LIKE ‘%text%‘ OR {p:description[de]:o} LIKE ‘%text%‘ |
在<selects>字段中的值指定要返回的数据库列。星号(*)将返回所有数据库列,如SQL约定。要搜索一个属性,指定大括号,如属性标识符:SELECT{code}FROM {Product}。
要获取本地化的属性值,使用的语言标识符作为后缀属性名在,包含在方括号([and]),如:
SELECT * FROM {Category}这个查询返回每个数据库列的Category表。
SELECT {pk},{code},{name[de]} FROM {Product}
从产品表查询返回的数据库pk,code,name[de]列.
<type>
该数值为<type>在FROM子句中字段中指定采用hybris Commerce Suite的类型,嵌套在大括号{and}这是要搜索,例如:
SELECT * FROM {Product} SELECT * FROM {Category JOIN Catalog}
你可以指定一个别名用于区分属性字段,使用AS运算符:
SELECT {p.code} FROM {Product AS p} ORDER BY {p.code}您也可以运行JOIN和LEFT JOIN的查询,如:
SELECT {cj.code} FROM {SyncItemCronJob AS sicj JOIN SyncItemJob AS sij ON {sicj:job} = {sij:pk} } SELECT {p1.PK},{p2.PK} FROM {Product AS p1 LEFT JOIN Product AS p2 ON {p1.code} = {p2.code} } WHERE {p1.PK} <> {p2.PK} ORDER BY {p1.code} ASC
一定要记住,这是最重要的,整个<type>块必须由封闭{和}不管多种类型.不要试图在多个放<type>块在FROM子句中。尽管这看起来是工作,它可能会导致不可预知的错误。
子类型
默认情况下,指定类型搜索导致FlexibleSearch查询来搜索该类型和任何子类型。例如,下面的代码片段返回代码和产品与VariantProduct的所有实例的的PK:
SELECT {code},{pk} FROM {Product}
标签:
原文地址:http://my.oschina.net/penfin/blog/471741