SetOffset and SetLimit
Select all nodes with primary type 'nt:unstructured' and returns only 3 nodes starting with the second node in list.
Common info
QueryImpl class has two methods: one to indicate how many results shall be returned at most, and another to fix the starting position.
- setOffset(long offset) - Sets the start offset of the result set.
- setLimit(long position) - Sets the maximum size of the result set.
Repository structure
Repository contains mix:title nodes, where jcr:title has different values.
- root
- node1 (nt:unstructured)
- node2 (nt:unstructured)
- node3 (nt:unstructured)
- node4 (nt:unstructured)
- node5 (nt:unstructured)
- node6 (nt:unstructured)
Query execution
SQL
// 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();
Fetch result
Lets get nodes:
NodeIterator it = result.getNodes();
if(it.hasNext())
{
Node findedNode = it.nextNode();
}
In usual case (without using setOffset and setLimit methods) Node iterator returns all nodes (node1...node6).
But in our case NodeIterator will return "node2","node3" and "node4".
[node1
node2 node3 node4 node5 node6]