3.1.4.1.4. Path constraints

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

Exact path constraint

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

Find a node with the 'nt:file' primary type that is located on the "/folder1/folder2/document1" exact path.

Repository structure

Repository filled by different nodes. There are several folders which contain other folders and files.

  • root

    • folder1 (nt:folder)

      • folder2 (nt:folder)

        • document1 (nt:file) // This document we want to find

        • folder3 (nt:folder)

          • document1 (nt:file)

Query execution

  • SQL

    // make SQL query
    
    QueryManager queryManager = workspace.getQueryManager();
    // we want find 'document1'
    String sqlStatement = "SELECT * FROM nt:file WHERE jcr:path = '/folder1/folder2/document1'";
    // create query
    Query query = queryManager.createQuery(sqlStatement, Query.SQL);
    // execute query and fetch result
    QueryResult result = query.execute();
  • XPath

    // make SQL query
    
    QueryManager queryManager = workspace.getQueryManager();
    // we want to find 'document1'
    String xpathStatement = "/jcr:root/folder1[1]/folder2[1]/element(document1,nt:file)[1]";
    // create query
    Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
    // execute query and fetch result
    QueryResult result = query.execute();

    Remark: The indexes [1] are used in order to get the same result as the SQL statement. SQL by default only returns the first node, whereas XPath fetches by default all nodes.

Fetching result

Let's get nodes:

NodeIterator it = result.getNodes();


if(it.hasNext())
{
   Node findedNode = it.nextNode();
}

NodeIterator will return expected "document1".

You can also get a table:

String[] columnNames = result.getColumnNames();

RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}

Table content is:

jcr:pathjcr:score
/folder1/folder2/document11030


Child node constraint

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

Find all nodes with the primary type 'nt:folder' that are children of node by the "/root1/root2" path. Only find children, do not find further descendants.

Repository structure

The repository is filled by "nt:folder" nodes. The nodes are placed in a multilayer tree.

  • root

    • 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 find

Query execution

  • SQL

    The use of "%" in the LIKE statement includes any string, therefore there is a second LIKE statement that excludes the string which contains "/". In this way, child nodes are included but descendant nodes are excluded.

    // make SQL query
    
    QueryManager queryManager = workspace.getQueryManager();
    // create query
    String sqlStatement = "SELECT * FROM nt:folder WHERE jcr:path LIKE '/folder1/folder2/%' AND NOT jcr:path LIKE '/folder1/folder2/%/%'";
    Query query = queryManager.createQuery(sqlStatement, Query.SQL);
    // execute query and fetch result
    QueryResult result = query.execute();
  • XPath

    // make XPath query
    
    QueryManager queryManager = workspace.getQueryManager();
    // create query
    String xpathStatement = "/jcr:root/folder1[1]/folder2[1]/element(*,nt:folder)";
    Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
    // execute query and fetch result
    QueryResult result = query.execute();

Fetching result

Let's get nodes:

NodeIterator it = result.getNodes();


if(it.hasNext())
{
   Node findedNode = it.nextNode();
}

The NodeIterator will return "folder3" and "folder5".

You can also get a table:

String[] columnNames = result.getColumnNames();

RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}

The table content is:

jcr:pathjcr:score
/folder1/folder2/folder31707
/folder1/folder2/folder51707


Finding all descendant nodes

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

Find all nodes with the 'nt:folder' primary type that are descendants of the "/folder1/folder2" node.

Repository structure

The repository contains "nt:folder" nodes. The nodes are placed in a multilayer tree.

  • root

    • 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 find

Query execution

  • SQL

    // make SQL query
    
    QueryManager queryManager = workspace.getQueryManager();
    // create query
    String sqlStatement = "SELECT * FROM nt:folder WHERE jcr:path LIKE '/folder1/folder2/%'";
    Query query = queryManager.createQuery(sqlStatement, Query.SQL);
    // execute query and fetch result
    QueryResult result = query.execute();
  • XPath

    // make XPath query
    
    QueryManager queryManager = workspace.getQueryManager();
    // create query
    String xpathStatement = "/jcr:root/folder1[1]/folder2[1]//element(*,nt:folder)";
    Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
    // execute query and fetch result
    QueryResult result = query.execute();

Fetching result

Let's get nodes:

NodeIterator it = result.getNodes();


if(it.hasNext())
{
   Node findedNode = it.nextNode();
}

The NodeIterator will return "folder3", "folder4" and "folder5" nodes.

You can also get a table:

String[] columnNames = result.getColumnNames();

RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}

Table content is:

jcr:pathjcr:score
/folder1/folder2/folder31000
/folder1/folder2/folder3/folder41000
/folder1/folder2/folder51000


Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus