Database Reference
In-Depth Information
mented in
“REST Server Parameters” on page 531
—but a common use case is to be able
to break the results of your query into smaller pages of results, so we will examine
how to achieve that here.
The REST Server provides a mechanism whereby you can send it an XQuery and
have it
cache
the results of that query. It will provide you with a
session identifier
,
which you can then use in subsequent requests to pull back subsets of those results
(i.e., pages).
For this example, let's imagine that we have added many more documents about peo‐
ple to our
/db/people
collection, and that this time we wish to find
the average age of
people in each settlement
. We know that there will be lots of results as our people live
all over the world, so we want to return the results
ordered by age ascending
; more
importantly, however, so as not to overwhelm the end user we want to present the
results in pages of
10 results at a time
.
Let's consider the XQuery that we might wish to
POST
to the REST Server API to ach‐
ieve this. Apart from it being a more complex XQuery, note that the
cache="yes"
,
start="1"
, and
max="10"
attributes are set on the
query
element. The
cache
attribute
instructs the REST Server to return a session identifier for the result set generated by
the query. In addition,
start
instructs the REST Server to return results from the
cached set starting at position
1
, and
max
instructs the server to return up to a maxi‐
mum of
10
results from the cached set:
<?xml version="1.0" encoding="UTF-8"?>
<query
xmlns=
"http://exist.sourceforge.net/NS/exist"
cache=
"yes"
start=
"1"
max=
"10"
>
<text>
<![CDATA[
xquery version "1.0";
for $settlement in distinct-values(/person/residence/location/settlement)
let $average-age := avg(
/person[residence/location/settlement eq $settlement]/born/(year-from-date(
current-date()) - year-from-date(xs:date(./date))))
order by $average-age ascending
return
<settlement>
<name>{$settlement}</name>
<average-age>{$average-age}</average-age>
</settlement>
]]>
</text>
</query>
We send our more complex query to the REST Server API in exactly the same way as
our simpler query, using this cURL command:
Search WWH ::
Custom Search