JCR Query Usecases

1 Intro

JCR supports two query languages - JCR and XPath. A query, whether XPath or SQL, specifies a subset of nodes within a workspace, called the result set. The result set constitutes all the nodes in the workspace that meet the constraints stated in the query.

2 Query Lifecycle

2.1 Query Creation and Execution

SQL

// get QueryManager
QueryManager queryManager = workspace.getQueryManager(); 
// make SQL query
Query query = queryManager.createQuery("SELECT * FROM nt:base ", Query.SQL);
// execute query
QueryResult result = query.execute();

XPath

// get QueryManager
QueryManager queryManager = workspace.getQueryManager(); 
// make XPath query
Query query = queryManager.createQuery("//element(*,nt:base)", Query.XPATH);
// execute query
QueryResult result = query.execute();

2.2 Query Result Processing

// fetch query result
QueryResult result = query.execute();

Now we can get result in an iterator of nodes:

NodeIterator it = result.getNodes();

or we get the result in a table:

// get column names
String[] columnNames = result.getColumnNames();
// get column rows
RowIterator rowIterator = result.getRows();
while(rowIterator.hasNext()){
   // get next row
   Row row = rowIterator.nextRow();
   // get all values of row
   Value[] values = row.getValues();
}

2.3 Scoring

The result returns a score for each row in the result set. The score contains a value that indicates a rating of how well the result node matches the query. A high value means a better matching than a low value. This score can be used for ordering the result.

eXo JCR Scoring is a mapping of Lucene scoring. For a more in-depth understanding, please study Lucene documentation.

jcr:score counted in next way - (lucene score)*1000f.

Score may be increased for specified nodes, see Index Boost Value

Also, see an example Order by Score

3 Query Examples

3.1 Query result settings

3.2 Type Constraints

3.3 Property Constraints

3.4 Path Constraint

3.5 Ordering specifing

3.6 Fulltext Search

3.7 Indexing rules and additional features

Recently Modified

Creator: Sergey Karpenko on 10/06/2009
Copyright (c) 2000-2009. Allright reserved - eXo platform SAS
1.6.13286