Loading TOC...

cts.remainder

cts.remainder(
   [$node as Node]
) as Number

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(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).

Example

const s = cts.search("dog");
const results = 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.

Example

// 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

Example

for (const a of cts.search("my test")) {
 if (cts.remainder(a) == 1) {
   xdmp.nodeUri(a);
 };
};

=> /test.json

Comments

    Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy