Fulltext Search by All Properties in Node
Find nodes with mixin type 'mix:title' where any property contains 'break' string.
Repository structure:
Repository filled with different nodes with mixin type 'mix:title' and different values of 'jcr:title' and 'jcr:description' properties.
- 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.'
Query execution
SQL
// make SQL query
QueryManager queryManager = workspace.getQueryManager();
String sqlStatement = "SELECT * FROM mix:title WHERE CONTAINS(*,'break')";
// 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 find 'document1'
String xpathStatement = "//element(*,mix:title)[jcr:contains(.,'break')]";
// create query
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();
Fetch result
Let's get nodes:
NodeIterator it = result.getNodes();
while(it.hasNext())
{
Node findedNode = it.nextNode();
}
NodeIterator will return "document1" and "document2".
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:title | jcr:description | ... | jcr:path |
|---|
| Prison break. | Run, Forest, run )) | ... | /document2 |
| Titanic | An iceberg breaks a ship. | ... | /document3 |