cts:remainder( [$node as node()] ) as xs:integer
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()
.
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).
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.
xdmp:document-insert("/test.xml", <a>my test</a>); for $x in cts:search(collection(),"my test") return cts:remainder($x) => 1
for $a in cts:search(collection(),"my test") where $a[cts:remainder() eq 1] return xdmp:node-uri($a) => /test.xml
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.