Fulltext Search by Property
Find all nodes containing a mixin type 'mix:title' and whose 'jcr:description' contains "forest" string.
Repository Structure
The repository is filled with nodes of the mixin type 'mix:title' and different values of the 'jcr:description' property.
- 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."
Query Execution
SQL
// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// we want find document which contains "forest" word
String sqlStatement = "SELECT \* FROM mix:title WHERE CONTAINS(jcr:description, 'forest')";
// 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 document which contains "forest" word
String xpathStatement = "//element(*,mix:title)[jcr:contains(@jcr:description, 'forest')]";
// create query
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();
}
NodeIterator will return "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:description | ... | jcr:path |
|---|
| The brown fox lives in forest. | ... | /document2 |