MarkLogic 12 Product Documentation
op:transitive-closure

op:transitive-closure(
   $plan as map:map,
   $start as item(),
   $end as item(),
   [$options as xs:string*|map:map?]
) as map:map

Summary

This method performs a transitive closure operation over a graph-like structure, identifying all reachable node pairs from a given start node to an end node through one or more intermediate steps. A set of (start, end) node pairs where a path exists between them with a length between minLength and maxLength, inclusive. This models the SPARQL one-or-more (+) operator, enabling recursive or chained relationships to be queried efficiently.

Parameters
$plan The Optic Plan. You can either use the XQuery => chaining operator or specify the variable that captures the return value from the previous operation.
$start The column is the starting node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function.
$end The column is the end node of the traversal. The column can be named with a string or a column function such as op:col, op:view-col, or op:schema-col, or constructed from an expression with the op:as function.
$options This is either an array of strings or an object containing keys and values for the options to this operator. Options include:
  • min-length

    This option is the minimum number of steps (edges) required in the path. It should be a non-negative integer, and the default is 1.

  • max-length

    This option Maximum number of steps (edges) allowed in the path. It should be a non-negative integer, and the default is unlimited.

Example

(: Insert data in SPARQL Update query format
PREFIX ex: <http://example.org/>
INSERT DATA
{
"Alice" ex:parent "Bob" .
"Bob" ex:parent "Carol" .
"Carol" ex:parent "David" .
"David" ex:parent "Eve" .
"Eve" ex:parent "Frank" .
"George" ex:parent "Helen" .
"Helen" ex:parent "Ian" .
"Alice" ex:parent "Cindy" .
"Cindy" ex:parent "John" .
};
:)

(: Calculate the node pairs with the number of edges between 4 and 5 :)
xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-triples((
   op:pattern(op:view-col("test","person"),sem:iri("http://example.org/parent"),op:col("ancestor"))
  ))
=>op:transitive-closure(op:view-col("test","person"),op:as("end",op:col("ancestor")),map:entry("min-length",4)=>map:with("max-length",5))
=>op:result()

(: Results as below
|test.person | ancestor |
|Alice       | Eve      |
|Bob         | Frank    |
|Alice       | Frank    |
:)

  

Example

(: Same setup as above. Use string-format options:)
(: Calculate the node pairs with the number of edges between 4 and 5 :)
xquery version "1.0-ml";
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-triples((
   op:pattern(op:view-col("test","person"),sem:iri("http://example.org/parent"),op:col("ancestor"))
  ))
=>op:transitive-closure(op:view-col("test","person"),op:as("end",op:col("ancestor")),("min-length=4","max-length=5"))
=>op:result()

(: Results as below
|test.person | ancestor |
|Alice       | Eve      |
|Bob         | Frank    |
|Alice       | Frank    |
:)
    
Powered by MarkLogic Server | Terms of Use | Privacy Policy