Understanding JCR Query search results

Oct 04, 2013


When you query the JCR repository using SQL2/Query Builder/XPath query, lucene handles the query in the backend. It orders the query based on a score it calculates. You can print the score using this code snippet

Code:

QueryManager qm = jcrSession.getWorkspace().getQueryManager();

String queryString = “SELECT * FROM [cq:PageContent] WHERE ” …..” ;

Query query = qm.createQuery(queryString, Query.JCR_SQL2);

// execute query

QueryResult result = query.execute();

// 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();

//print the values. One of the values is jcr_score

}

The Score is calculated using pretty complex mathematical formula. You can read more about it at http://lucene.apache.org/core/3_6_2/api…ilarity.html

Some more explanation is provided at http://www.lucenetutorial.com/advanced-t…oring.html

Here are some main points:

  • The length of document is number of terms not number of characters
  • The relationship between frequency and length is not linear. It is sqrt(frequency/total terms in document)
  • If your search has more than one term and one of the terms is a common term in the repository, then that common word gets less weightage. For e.g. if your content is for the company Polycom, then term like ‘conference might be a common term. So, when you search for ‘VOIP Conference’, then ‘conference’ would get lower weightage.

If you would like more help optimizing or managing your JCR Query, Contact Us Today.

ABOUT TECHASPECT

TechAspect is an innovative digital transformation agency, specializing in delivering the digital experience, commerce, and marketing solutions. For nearly two decades, we have been helping traditional businesses transform and create dynamic digital cultures through disruptive strategies and agile deployment of innovative solutions.