Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created January 16, 2015 08:39
Show Gist options
  • Save ableasdale/15e0b87cd2a7710c4c21 to your computer and use it in GitHub Desktop.
Save ableasdale/15e0b87cd2a7710c4c21 to your computer and use it in GitHub Desktop.
MarkLogic Plan Manager (Incomplete)
xquery version "1.0-ml";
declare variable $path-segment := "E:\plan-docs\";
declare variable $query := xdmp:plan(cts:search(doc(), cts:word-query("test")));
declare variable $query2 := xdmp:plan(cts:search(doc(), cts:word-query(("test", "time", "language", "gas", "police", "politics"))));
declare function local:get-distinct-term-keys($query) as xs:unsignedLong* {
distinct-values($query//xs:unsignedLong(*:key))
};
(: local:get-distinct-values() :)
declare function local:get-cts-term($key as xs:unsignedLong?) as element(cts:term)? {
let $options := <options xmlns="cts:train"><use-db-config>true</use-db-config><details>true</details></options>
let $id := $key
let $doc := ( cts:search(doc(), cts:term-query($id)) )[1]
return cts:hash-terms($doc, $options)//cts:term[@id = $id]
};
declare function local:get-counts-for-cts-term($term) {
fn:count( cts:search(doc(), cts:query($term/node())) )
};
declare function local:get-matching-uris-for-term($term) {
cts:uris((), (), cts:query($term/node()) )
};
(:
declare function local:save-docs-for-term() {
};
:)
(: Main :)
(: Example one -- resolve cts terms by key
for $term-key in local:get-distinct-term-keys($query2)
return ($term-key, local:get-cts-term($term-key) )
:)
(: Example two -- resolve counts for a group of term keys
for $term-key in local:get-distinct-term-keys($query2)
return local:get-counts-for-cts-term( local:get-cts-term($term-key) )
:)
(: Example three - resolve all matching URIs for a group of term keys
for $term-key in local:get-distinct-term-keys($query2)
return ($term-key, local:get-matching-uris-for-term( local:get-cts-term($term-key) ) )
:)
(: Example four - resolve (and save) all docs for a group of matching term keys :)
for $term-key in local:get-distinct-term-keys($query2)
(: return ($term-key, local:get-matching-uris-for-term( local:get-cts-term($term-key) ) ) :)
return
(: xdmp:filesystem-directory-create( concat($path-segment,$term-key)) :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment