find marklogic hosts one by one

27 Views Asked by At

I am writing below code :-

xquery version "1.0-ml";
declare variable $hosts := xdmp:host-name(xdmp:group-hosts(xdmp:group()));
let $map := map:map()
let $i := 1
for $h in $hosts
let $key := map:put($map, "$i", $h)
return 
for $x in map:keys($map) 
order by $x return
map:get($map, $x)

The above code returns me 4 hosts, host1.com, host2.com and host3.com I want to write the above code in such way that if I pass 1 it should return me host1.com or if i pass 3 it should return host3.com. how to achieve that?

1

There are 1 best solutions below

0
rjrudin On

If you only need the host name, all you need to do is:

xdmp:host-name(xdmp:group-hosts(xdmp:group()))[$index]

Where $index would be the argument that you provide. You can put that into a library module by declaring a function:

declare function get-host($index as xs:integer) {
  xdmp:host-name(xdmp:group-hosts(xdmp:group()))[$index]
};

See https://docs.marklogic.com/guide/app-dev/import_modules#id_26329 for more information about XQuery modules.