Loading TOC...

MarkLogic 9 Product Documentation
DocumentsSearch.reduce

DocumentsSearch.reduce(
   reducer as function,
   seed as anyValue
) as DocumentsSearch

Summary

Specifies a function similar to Array.prototype.reduce() to apply to each document within the slice. See the Usage Notes for details.

Parameters
reducer The reducing function.
seed The initial reduced value passed to the reducing function.

Usage Notes

The reducer function is called once for each matched document. The value returned by the last call becomes the final reduced result. The reducer function should have the following signature:

function (prev, current, index, state)

Where the parameters contain the following:

prev
The output returned by the previous call, or the seed value on the first call.
current
The current document.
index
The number of the current document.
state
A state object with a boolean-valued isLast> property that indicated the last call. You can set it to true to prematurely halt the reduction.

You cannot use this method in conjunction with DocumentsSearch.map.

See Also

Example


// Use a custom reducer, with an initial seed value
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
  .where(cts.jsonPropertyValueQuery('author','Mark Twain'))
  .reduce(function (prev, match, index, state) {
    // do something
  }, {count: 0, value: 0, matches: []})
  .result()
   

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