Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Created December 5, 2015 17:02
Show Gist options
  • Select an option

  • Save ableasdale/876b6f682f49d1e9d177 to your computer and use it in GitHub Desktop.

Select an option

Save ableasdale/876b6f682f49d1e9d177 to your computer and use it in GitHub Desktop.
Pattern for storing contents of a query in a server-side map in MarkLogic Server
xquery version "1.0-ml";
declare function local:get-map-by-name($name) {
if (
fn:exists(xdmp:get-server-field($name))
) then (
xdmp:log(fn:concat("A server-side map with the following name (", $name, ") exists - returning it")), xdmp:get-server-field($name)
) else (
xdmp:log(fn:concat("No server-side map was found with the name (", $name, ") - creating it")), xdmp:set-server-field($name, map:map())
)
};
declare function local:get-docs-by-id-group($group as xs:string, $map-name as xs:string) {
let $map := local:get-map-by-name($map-name)
return
let $_ :=
for $x in cts:search(fn:doc(), cts:element-value-query(xs:QName("id"), $group))
return
if (
map:contains($map, xdmp:node-uri($x))
) then (
xdmp:log(fn:concat("Map: (", $map-name, ") contains uri: (", xdmp:node-uri($x), ")")),
map:get($map, xdmp:node-uri($x))
) else (
xdmp:log(fn:concat("Map: (", $map-name, ") does not contain uri: (", xdmp:node-uri($x), ")")),
map:put($map, xdmp:node-uri($x), xdmp:quote($x/node()))
)
return
xdmp:set-server-field($map-name, $map)
};
local:get-docs-by-id-group("4", "my-map")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment