Loading TOC...

MarkLogic 12 EA 1 Product Documentation
cts:remainder

cts:remainder(
   [$node as node()]
) as xs:integer

Summary

Returns an estimated search result size for a node, or of the context node if no node is provided. The search result size for a node is the number of fragments remaining (including the current node) in the result sequence containing the node. This is useful to quickly estimate the size of a search result sequence, without using fn:count() or xdmp:estimate().

Parameters
node A node. Typically this is an item in the result sequence of a cts:search operation. If you specify the first item from a cts:search expression, then cts:remainder will return an estimate of the number of fragments that match that expression.

Usage Notes

This function makes it efficient to estimate the size of a search result and execute that search in the same query. If you only need an estimate of the size of a search but do not need to run the search, then xdmp:estimate is more efficient.

To return the estimated size of a search with cts:remainder , use the first item of a cts:search result sequence as the parameter to cts:remainder. For example, the following query returns the estimated number of fragments that contain the word "dog":

  cts:remainder(cts:search(collection(), "dog")[1]) 

When you put the position predicate on the cts:search result sequence, MarkLogic Server will filter all of the false-positive results up to the specified position, but not the false-positive results beyond the specified position. Because of this, when you increase the position number in the parameter, the result from cts:remainder might decrease by a larger number than the increase in position number, or it might not decrease at all. For example, if the query above returned 10, then the following query might return 9, it might return 10, or it might return less than 9, depending on how the results are dispersed throughout different fragments:

  cts:remainder(cts:search(collection(), "dog")[2]) 

If you run cts:remainder on a constructed node, it always returns 0; it is primarily intended to run on nodes that are the retrieved from the database (an item from a search result or an item from the result of an XPath expression that searches through the database).

Example

let $x := cts:search(collection(), "dog")
return
(cts:remainder($x[1]), $x)

=> Returns the estimated number of items in the search
   for "dog" followed by the results of the search.

Example

xdmp:document-insert("/test.xml", <a>my test</a>);
for $x in cts:search(collection(),"my test")
return cts:remainder($x)
=> 1

Example

for $a in cts:search(collection(),"my test")
where $a[cts:remainder() eq 1]
return xdmp:node-uri($a)
=> /test.xml

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.