Created
May 16, 2017 18:09
-
-
Save masyukun/a0d09e2fe4ab3e1ebbda32a0491aaaf1 to your computer and use it in GitHub Desktop.
Generate a treemap data structure for visualization with highcharts of files in a MarkLogic database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
module namespace treemap = "http://matthewroyal.com/marklogic/ferret/filetypetreemap"; | |
declare variable $sampleSize := 100; | |
declare variable $sampleThreshold := 1000; | |
declare function treemap:get( | |
$context as map:map, | |
$params as map:map | |
) as document-node()* | |
{ | |
let $numDocs := xdmp:estimate(cts:search(/, cts:and-query(()))) | |
let $docTypes := | |
fn:distinct-values( | |
if ($numDocs gt $sampleThreshold) then | |
(: Just get a sample :) | |
for $d in cts:search(/, cts:and-query(()), "score-random" )[1 to $sampleSize] | |
return fn:node-name($d/element()) | |
else | |
for $d in cts:search(/, cts:and-query(())) | |
return fn:node-name($d/element()) | |
) | |
let $out := | |
"[" || fn:string-join(( | |
for $p at $i in | |
(: Order by frequency:) | |
for $d in $docTypes | |
let $dest := xdmp:estimate(cts:search(/, cts:and-query((cts:element-query($d, cts:and-query(()) ))) )) | |
order by $dest descending | |
return $d||","||$dest | |
let $pieces := fn:tokenize($p, ",") | |
let $d := $pieces[1] | |
let $dest := $pieces[2] | |
return "{ name: '" || $d || "', value: " || $dest || ", colorValue: " || $i || " }" | |
), ",") || "]" | |
(: JSON return :) | |
let $_ := map:put($context, "output-types", "application/json") | |
let $_ := xdmp:set-response-code(200, "OK") | |
return document { $out } | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment