Multivalue Property Comparison
Find all nodes with the primary type 'nt:unstructured' whose property 'multiprop' contains both values "one" and "two".
Repository Structure
The repository contains nt:unstructured nodes with different 'multiprop' properties.
- root
- node1 (nt:unstructured) multiprop = ["one","two"]
- node1 (nt:unstructured) multiprop = ["one","two","three"]
- node1 (nt:unstructured) multiprop = ["one","five"]
Query Execution
SQL
// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// create query
String sqlStatement = "SELECT * FROM nt:unstructured WHERE multiprop = 'one' AND multiprop = 'two'";
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 = "//element(*,nt:unstructured)[@multiprop = 'one' and @multiprop = 'two']";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();
Fetching the Result
Let's get nodes:
NodeIterator it = result.getNodes();
if(it.hasNext())
{
Node findedNode = it.nextNode();
}
The NodeIterator will return "node1" and "node2".
We 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:primarytype | jcr:path | jcr:score |
|---|
| nt:unstructured | /node1 | 3806 |
| nt:unstructured | /node2 | 3806 |