March 2018
Intermediate to advanced
34 pages
1h 33m
English
The Optic API, introduced in MarkLogic 9, implements common relational operations over data extracted from documents. This chapter illustrates how to accomplish some common tasks, which should help in transitioning to MarkLogic from a relational background.
An Optic API query returns a large result set. Get a stable set of results a page at a time.
Applies to MarkLogic versions 9+ and higher
In its simplest form:
constop=require('/MarkLogic/optic');constpageSize=...;constpageNumber=...;op.fromTriples([...)]).offsetLimit(op.param('offset'),pageSize).result(null,{offset:pageSize*(pageNumber-1)})
Expanded out to a main module, which allows the caller to specify the page and a timestamp, the recipe looks like this:
constop=require('/MarkLogic/optic');constpageNumber=parseInt(xdmp.getRequestField('page','1'),10);constpageSize=1000;lettimestamp=xdmp.getRequestField('timestamp');if(timestamp===null){timestamp=xdmp.requestTimestamp();}constresponse={timestamp:timestamp,page:pageNumber,results:xdmp.invokeFunction(function(){returnop.fromTriples([...]).offsetLimit(op.param('offset'),pageSize).result(null,{offset:pageSize*(pageNumber-1)});},{timestamp:timestamp})}response
xdmp:timestamp
Sometimes your result set will be bigger than you want to return in a single request. Paging solves this problem ...
Read now
Unlock full access