Last active
August 29, 2015 14:06
-
-
Save joemfb/9557ab3db61ae30d010f to your computer and use it in GitHub Desktop.
Utility functions for working with app-service APIs (Search, REST, etc.)
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"; | |
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; | |
import module namespace ast = "http://marklogic.com/appservices/search-ast" at "/MarkLogic/appservices/search/ast.xqy"; | |
import module namespace dbut = "http://marklogic.com/rest-api/lib/db-util" at "/MarkLogic/rest-api/lib/db-util.xqy"; | |
import module namespace eput = "http://marklogic.com/rest-api/lib/endpoint-util" at "/MarkLogic/rest-api/lib/endpoint-util.xqy"; | |
import module namespace sut = "http://marklogic.com/rest-api/lib/search-util" at "/MarkLogic/rest-api/lib/search-util.xqy"; | |
import module namespace csu = "http://marklogic.com/rest-api/config-query-util" at "/MarkLogic/rest-api/lib/config-query-util.xqy"; | |
import module namespace config-query = "http://marklogic.com/rest-api/models/config-query" at "/MarkLogic/rest-api/models/config-query-model.xqy"; | |
declare namespace search = "http://marklogic.com/appservices/search"; | |
declare function local:get-server() | |
{ | |
local:get-server("Default") | |
}; | |
declare function local:get-server($group) | |
{ | |
let $server := xdmp:server-name(xdmp:server()) | |
return | |
if ($server ne "App-Services") | |
then $server | |
else | |
let $config := admin:get-configuration() | |
let $groupid := admin:group-get-id($config, $group) | |
let $db := xdmp:database() | |
for $id in admin:group-get-appserver-ids($config, $groupid) | |
where admin:appserver-get-database($config, $id) eq $db and | |
admin:appserver-get-type($config, $id) eq "http" | |
return admin:appserver-get-name($config, $id) | |
}; | |
declare function local:sq-from-json($json as json:object) as element(search:query)? | |
{ | |
sut:search-from-json( xdmp:to-json($json) )/search:query | |
}; | |
declare function local:sq-to-json($sq as element(search:query)) as json:object? | |
{ | |
csu:xml-to-json($sq) ! xdmp:from-json(.) | |
}; | |
declare function local:sq-to-cts($sq as element(search:query), $options as element(search:options)) as cts:query? | |
{ | |
map:get(ast:to-query($sq, $options), "query") | |
}; | |
declare function local:named-options($name as xs:string) as element(search:options)? | |
{ | |
(: | |
Doesn't work in QC, works elsewhere: | |
sut:options(map:entry("options", $name)) | |
:) | |
let $uri := eput:make-document-uri($config-query:storage-prefix || $name, (), local:get-server()) | |
return dbut:access-config(function() { fn:doc($uri)/node() }) | |
}; | |
let $options := local:named-options("all") | |
let $q := | |
xdmp:from-json('{"query":{"queries":[ | |
{"and-query":{"queries":[{"range-constraint-query":{"constraint-name":"salary","value":["$750K - $1M"]}}]}}, | |
{"operator-state":{"operator-name":"results","state-name":"compact"}}]}}') | |
let $sq := local:sq-from-json($q) | |
return ( | |
$sq, | |
local:sq-to-json($sq), | |
local:sq-to-cts($sq, $options) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment