This chapter describes the temporal search features, and includes the following sections:
The following table summarizes the available functions used to construct cts:query
expressions for searching temporal documents. For general details on constructing cts:query
expressions, see Composing cts:query Expressions in the Search Developer's Guide.
This section describes the Allen and ISO SQL algebra operators that can be used in search queries. Temporal queries are basically some interval operations on time period such as, period equalities, containment and overlaps. MarkLogic Server supports both Allen and SQL operators when comparing time periods. Allen's interval algebra provides the most comprehensive set of these operations. SQL 2011 also provides similar operators. However all the SQL operators can be expressed using Allen's Algebra.
In general, Allen operators, which are identified with an ALN_ prefix, are more restrictive than ISO SQL operators, which are identified with an ISO_ prefix. The illustration below shows the relationships between the X and Y periods for each operator as used in the following period queries:
cts:period-range-query(X, operator, Y)
where X is an axis and Y is a period.
cts:period-compare-query(X, operator, Y)
cts:period-compare(X, operator, Y)
Similar to Allen operators, SQL 2011 operators can also be mapped MarkLogic range index operations. The following illustrations show the mapping from ISO SQL operators to a composition of Allen Operators.
Note that the ALN operators described in Allen Operators each have only one X/Y period relationship, whereas some of the less restrictive ISO SQL operators have multiple X/Y period relationships. Such ISO SQL operators have the effect of multiple Allen operators, as shown below.
The range query on periods can be used only when there are range indexes for them on the database. However you may need to query based either on some external period values or those from a document. The following example shows how you can use the cts:period-compare function to determine whether two period values satisfy the conditions imposed by the comparison operator, ALN_MEETS
. If the ALN_MEETS
conditions are satisfied, then true
is returned; otherwise false
is returned.
var period1 = cts.period(xs.dateTime("2000-05-31T09:30:10"), xs.dateTime("2003-05-31T12:30:00")); var period2 = cts.period(xs.dateTime("2003-05-31T12:30:00"), xs.dateTime("2004-05-31T14:30:00")); cts.periodCompare(period1, "ALN_MEETS", period2);
xquery version "1.0-ml"; let $period1 := cts:period(xs:dateTime("2000-05-31T09:30:10"), xs:dateTime("2003-05-31T12:30:00")) let $period2 := cts:period(xs:dateTime("2003-05-31T12:30:00"), xs:dateTime("2004-05-31T14:30:00")) return cts:period-compare($period1,"ALN_MEETS",$period2)
This section describes some sample search queries. The searches described in this section are done on the documents described in Example: The Lifecycle of a Temporal Document.
The following query searches for the temporal documents that have a valid end time before 14:00
:
cts.search(cts.periodRangeQuery( "valid", "ALN_BEFORE", cts.period(xs.dateTime("2014-04-03T14:00:00"), xs.dateTime("9999-12-31T11:59:59Z")) ))
cts:search(fn:doc(), cts:period-range-query( "valid", "ALN_BEFORE", cts:period(xs:dateTime("2014-04-03T14:00:00"), xs:dateTime("9999-12-31T11:59:59Z")) ))
This query returns Splits 1, 2 and 3 of the document.
The following query searches the temporal documents, using the cts:and-query to AND two cts:period-range-query functions, to locate the documents that represented the order at 11:30 when queried at 11:51.
cts.search(cts.andQuery( [cts.periodRangeQuery( "system", "ISO_CONTAINS", cts.period(xs.dateTime("2014-04-03T11:51:00"), xs.dateTime("2014-04-03T11:51:01"))), cts.periodRangeQuery( "valid", "ISO_CONTAINS", cts.period(xs.dateTime("2014-04-03T11:30:00"), xs.dateTime("2014-04-03T11:30:01")))]))
xquery version "1.0-ml"; cts:search(fn:doc(), cts:and-query(( cts:period-range-query( "system", "ISO_CONTAINS", cts:period(xs:dateTime("2014-04-03T11:51:00"), xs:dateTime("2014-04-03T11:51:01"))), cts:period-range-query( "valid", "ISO_CONTAINS", cts:period(xs:dateTime("2014-04-03T11:30:00"), xs:dateTime("2014-04-03T11:30:01"))))))
This query returns Version 2 of the document.
The following query searches for the temporal documents that have a valid end time of 12:10
:
var period = cts.period(xs.dateTime("2014-04-03T12:10:00"), xs.dateTime("2014-04-03T13:10:00")); cts.search(cts.periodRangeQuery("valid", "ALN_MEETS", period))
xquery version "1.0-ml"; let $period := cts:period(xs:dateTime("2014-04-03T12:10:00"), xs:dateTime("2014-04-03T13:10:00")) return cts:search(fn:doc(), cts:period-range-query( "valid", "ALN_MEETS", $period))