cts.remainder( [node as Node] ) as Number
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
cts.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(fn.subsequence(cts.search("dog"), 1, 1))
When you take a subsequence of 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 number of results in the
subsequence, 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(fn.subsequence(cts.search("dog"), 2, 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).
const s = cts.search("dog"); const resultsults = new Array(); const clone = s.clone(); for (const x of fn.subsequence(s, 1, 1) ) { results.push(cts.remainder(x)); }; results.push(clone); results; => Returns the estimated number of items in the search for "dog" followed by the results of the search.
// Assume a document exist as follows: // declareUpdate(); // xdmp.documentInsert("/test.json", {"a":"my test"}); for (const x of fn.subsequence(cts.search("my test"), 1, 1) ) { cts.remainder(x); }; => 1
for (const a of cts.search("my test")) { if (cts.remainder(a) == 1) { xdmp.nodeUri(a); }; }; => /test.json
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.