How to do a date range search

Question

Your application might require a search based on date ranges, e.g. search for all nodes that have been created in the last 3 days.

How is it possible to do a date range search?

Answer

CRX offers the possibility to express date ranges using the xs:dateTime mapping. Following example shows how to search for nodes beneath /content/mypage that have been created between 2009/04/24 and 2009/04/28:

/jcr:root/content/mypage//*[@jcr:created >= xs:dateTime('2009-04-24T00:00:00.000+02:00') 
    and @jcr:created < xs:dateTime('2009-04-28T00:00:00.000+02:00')] 

The Lucene search engine indexes the actual date in milliseconds, that is the reason why the mapping using the xs:dateTime function is required in the date range search. The formatted string passed to this function should be obtained as shown in the following code excerpt:

// obtain javax.jcr.ValueFactory from JCR session 
ValueFactory factory = session.getValueFactory(); 
 
// obtain Calendar object 
Calendar cal = Calendar.getInstance(); 
 
// create javax.jcr.Value object of PropertyType.DATE 
Value dateValue = factory.createValue(cal); 
 
// finally, get string representation of the DATE-value which can be used in xs:dateTime function 
String dateString = dateValue.getString(); 
Adobe logó

Bejelentkezés a fiókba