// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// create query
String sqlStatement = "SELECT * FROM nt:unstructured";
QueryImpl query = (QueryImpl)queryManager.createQuery(sqlStatement, Query.SQL);
//return starting with second result
query.setOffset(1);
// return 3 results
query.setLimit(3);
// execute query and fetch result
QueryResult result = query.execute();
String sqlStatement = "SELECT * FROM nt:base";
root
document1 primarytype = "nt:unstructured" mixintype = "mix:title"
document2 primarytype = "nt:file" mixintype = "mix:lockable"
document3 primarytype = "nt:file" mixintype = "mix:title"
String sqlStatement = "SELECT * FROM nt:file";
结果:will return "document2" and "document3"
String sqlStatement = "SELECT * FROM mix:title";
结果:will return "document1" and "document3".
查询 mixin type "mix:title" 的prop_pagecount >90的节点
root
document1 (mix:title) jcr:title="War and peace" prop_pagecount=1000
document2 (mix:title) jcr:title="Cinderella" prop_pagecount=100
document3 (mix:title) jcr:title="Puss in Boots" prop_pagecount=60
String sqlStatement = "SELECT jcr:title FROM mix:title WHERE prop_pagecount < 90";
结果:The NodeIterator will return "document3".
查询 mixin type "mix:title" 的并且title以'P' 开头的的节点
root
document1 (mix:title) jcr:title="Star wars" jcr:description="Dart rules!!"
document2 (mix:title) jcr:title="Prison break" jcr:description="Run, Forest, run ))"
document3 (mix:title) jcr:title="Panopticum" jcr:description="It's imagine film"
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:title LIKE 'P%'";
结果:The NodeIterator will return "document2" and "document3".
root
document1 (mix:title) jcr:title="Star wars" jcr:description="Dart rules!!"
document2 (mix:title) jcr:title="P%rison break" jcr:description="Run, Forest, run ))"
document3 (mix:title) jcr:title="Panopticum" jcr:description="It's imagine film"
转义符号自己定义
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:title LIKE 'P#%ri%' ESCAPE '#'";
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:title LIKE 'P\%ri%' ESCAPE '\'";
结果:NodeIterator will return "document2".
查询type 是 'mix:title' 标题中不以P开头的节点
root
document1 (mix:title) jcr:title="Star wars" jcr:description="Dart rules!!"
document2 (mix:title) jcr:title="Prison break" jcr:description="Run, Forest, run ))"
document3 (mix:title) jcr:title="Panopticum" jcr:description="It's imagine film"
String sqlStatement = "SELECT * FROM mix:title WHERE NOT jcr:title LIKE 'P%'";
结果:NodeIterator will return "document1".
查询type 是 'mix:title' 并且 'jcr:description' 等于"fairytale" 并且 "prop_pagecount" 小于 90.
root
document1 (mix:title) jcr:title="War and peace" jcr:description="novel" prop_pagecount=1000
document2 (mix:title) jcr:title="Cinderella" jcr:description="fairytale" prop_pagecount=100
document3 (mix:title) jcr:title="Puss in Boots" jcr:description="fairytale" prop_pagecount=60
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:description = 'fairytale' AND prop_pagecount > 90";
结果:NodeIterator will return "document2".
查询 type是'mix:title' 'jcr:title' 等于 "Cinderella"或者"jcr:description" 等于 "novel".
root
document1 (mix:title) jcr:title="War and peace" jcr:description="novel"
document2 (mix:title) jcr:title="Cinderella" jcr:description="fairytale"
document3 (mix:title) jcr:title="Puss in Boots" jcr:description="fairytale"
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:title = 'Cinderella' OR jcr:description = 'novel'";
结果:NodeIterator will return "document1" and "document2".
查询 type是'mix:title' jcr:description 为空.
root
document1 (mix:title) jcr:title="Star wars" jcr:description="Dart rules!!"
document2 (mix:title) jcr:title="Prison break" jcr:description="Run, Forest, run ))"
document3 (mix:title) jcr:title="Titanic"
String sqlStatement = "SELECT * FROM mix:title WHERE jcr:description IS NULL";
结果:NodeIterator will return "document3".
查询 type是'mix:title' 'jcr:title' 为不分大小写的 'casesensitive' .
root
document1 (mix:title) jcr:title="CaseSensitive"
document2 (mix:title) jcr:title="casesensitive"
document3 (mix:title) jcr:title="caseSENSITIVE"
String sqlStatement = "SELECT * FROM mix:title WHERE UPPER(jcr:title) = 'CASESENSITIVE'";
String sqlStatement = "SELECT * FROM mix:title WHERE LOWER(jcr:title) = 'casesensitive'";
两条语句返回相同结果:
NodeIterator will return "document1", "document2" and "document3" (in all examples).
查询 primary type 是"nt:resource" jcr:lastModified 的值大于2006-06-04 并且小于2008-06-04.
root
document1 (nt:file)
jcr:content (nt:resource) jcr:lastModified="2006-01-19T15:34:15.917+02:00"
document2 (nt:file)
jcr:content (nt:resource) jcr:lastModified="2005-01-19T15:34:15.917+02:00"
document3 (nt:file)
jcr:content (nt:resource) jcr:lastModified="2007-01-19T15:34:15.917+02:00"
引入时间戳
// create query
StringBuffer sb = new StringBuffer();
sb.append("select * from nt:resource where ");
sb.append("( jcr:lastModified >= TIMESTAMP '");
sb.append("2006-06-04T15:34:15.917+02:00");
sb.append("' )");
sb.append(" and ");
sb.append("( jcr:lastModified <= TIMESTAMP '");
sb.append("2008-06-04T15:34:15.917+02:00");
sb.append("' )");
String sqlStatement = sb.toString();
返回结果:NodeIterator will return "/document3/jcr:content".
查询primary type 为 'nt:file'并且节点名称为'document'.
root
document1 (nt:file)
file (nt:file)
somename (nt:file)
函数 fn:name()
String sqlStatement = "SELECT * FROM nt:file WHERE fn:name() = 'document'";
返回结果:return the node whose fn:name equals "document".
查询primary type 为'nt:unstructured' 属性 'multiprop' 值既包含 "one" 又包含 "two"
root
node1 (nt:unstructured) multiprop = [ "one","two" ]
node2 (nt:unstructured) multiprop = [ "one","two","three" ]
node3 (nt:unstructured) multiprop = [ "one","five" ]
String sqlStatement = "SELECT * FROM nt:unstructured WHERE multiprop = 'one' AND multiprop = 'two'";
返回结果:The NodeIterator will return "node1" and "node2".
查询primary type 为'nt:file' 位于精确路径"/folder1/folder2/document1"下的
root
folder1 (nt:folder)
folder2 (nt:folder)
document1 (nt:file) // This document we want to find
folder3 (nt:folder)
document1 (nt:file)
String sqlStatement = "SELECT * FROM nt:file WHERE jcr:path = '/folder1/folder2/document1'";返回结果:The NodeIterator will return expected "document1".
查询primary type 为'nt:folder' 位于路径"/folder1/folder2"下的子节点,但不返回更深一级节点
rroot
folder1 (nt:folder)
folder2 (nt:folder)
folder3 (nt:folder) // This node we want to find
folder4 (nt:folder) // This node is not child but a descendant of '/folder1/folder2/'.
folder5 (nt:folder) // This node we want to
String sqlStatement = "SELECT * FROM nt:folder WHERE jcr:path LIKE '/folder1/folder2/%' AND NOT jcr:path LIKE '/folder1/folder2/%/%'";
返回结果:The NodeIterator will return "folder3" and "folder5".
查询primary type 为'nt:folder' 位于路径"/folder1/folder2"下的子孙节点
rroot
folder1 (nt:folder)
folder2 (nt:folder)
folder3 (nt:folder) // This node we want to find
folder4 (nt:folder) // This node we want to find
folder5 (nt:folder) // This node we want to
String sqlStatement = "SELECT * FROM nt:folder WHERE jcr:path LIKE '/folder1/folder2/%' ";
返回结果:The NodeIterator will return "folder3", "folder4" and "folder5" nodes.
查询mixin type 为'mix:title' 根据 prop_pagecount 大小排序
root
document1 (mix:title) jcr:title="War and peace" jcr:description="roman" prop_pagecount=4
document2 (mix:title) jcr:title="Cinderella" jcr:description="fairytale" prop_pagecount=7
document3 (mix:title) jcr:title="Puss in Boots" jcr:description="fairytale" prop_pagecount=1
String sqlStatement = "SELECT * FROM mix:title ORDER BY prop_pagecount ASC";
返回结果:The NodeIterator will return nodes in the following order "document3", "document1", "document2".
查询mixin type 为'mix:title' ,包含词{'brown','fox','jumps'}按分值排序。
root
document1 (mix:title) jcr:description="The quick brown fox jumps over the lazy dog."
document2 (mix:title) jcr:description="The brown fox lives in the forest."
document3 (mix:title) jcr:description="The fox is a nice animal."
String sqlStatement = "SELECT * FROM mix:title WHERE CONTAINS(*, 'brown OR fox OR jumps') ORDER BY jcr:score() ASC";
返回结果:will return nodes in the following order: "document3", "document2", "document1".
查询mixin type 为'mix:title' ,jcr:description包含词"forest"。
root
document1 (mix:title) jcr:description = "The quick brown fox jumps over the lazy dog."
document2 (mix:title) jcr:description = "The brown fox lives in a forest." // This is the node we want to find
document3 (mix:title) jcr:description = "The fox is a nice animal."
document4 (nt:unstructured) jcr:description = "There is the word forest, too."
String sqlStatement = "SELECT \* FROM mix:title WHERE CONTAINS(jcr:description, 'forest')";
返回结果:will return "document2".
查询mixin type 为'mix:title' ,任何属性值中包含词"break"。
root
document1 (mix:title) jcr:title ='Star Wars' jcr:description = 'Dart rules!!'
document2 (mix:title) jcr:title ='Prison break' jcr:description = 'Run, Forest, run ))'
document3 (mix:title) jcr:title ='Titanic' jcr:description = 'An iceberg breaks a ship.'
String sqlStatement = "SELECT * FROM mix:title WHERE CONTAINS(*,'break')";
返回结果:will return "document2" and "document3".
é,è,à等重音进行标准化
参考网址:
https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-jcr-query-usecases.html#JCR.OrderByScore