Loading TOC...

TuplesSearch.map

TuplesSearch.map(
   $mapper as objectOrFuncRef
) as TuplesSearch

Summary

Specifies a function similar to Array.prototype.map() to apply to each tuple within the slice or the configuration for the built-in mapper.

Parameters
$mapper A configuration object for the built-in mapper, or a reference to a custom mapper function. See the Usage Notes for details.

Usage Notes

You can pass in either a configuration object for the built-in mapper or a function reference to a custom mapper. The built-in mapper configuration object can have the following properties:

frequency
One of the string values "fragment", "item", or "none" (default). This setting controls whether to report the number of documents. That is, the fragments) containing the tuple, the number of occurrences for each tuple (the items), or to omit the frequency (which is the default for a tuples search).
names
Specify a name for each index or lexicon in the tuple and for the frequency (if enabled). If names are specified, the built-in mapper returns an object for each tuple with the names as properties. If the frequency is specified but not the name, the built-in mapper returns an array for each tuple. Use the same order for the names as the order in which you specify the lexicons or indices in jsearch.tuples. If you request frequency data, supply the name for the frequency property last. See the example, below.

An empty the configuration object causes the built-in mapper to return the tuple array node unchanged.

A custom mapper should have the following signature:

function (currentItem)

Where the single input is the current tuple to act on. If the function returns a value, the value is pushed on to the results array.

You cannot use this method in conjunction with TuplesSearch.reduce.

See Also

Example


// Configure the built-in mapper to return frequency, and specify
// names for each value in the tuple
var jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.tuples(['author','format','price'])
  .slice(0,2)
  .map({frequency: 'item', names: ['Author','Format','Price','Freq']})
  .result()

==> If the default output would be:
    [["John Steinbeck", "audiobook", 16], ["John Steinbeck", "hardback", 20]]
    Then the output from the built-in mapper as configured is the following:

[{"Author":"John Steinbeck", 
  "Format":"audiobook", 
  "Price":16, 
  "Freq":1
 },{
  "Author":"John Steinbeck", 
  "Format":"hardback", 
  "Price":20, 
  "Freq":1
}]
   

Example


// Supply a custom mapper. This one simply recasts the tuple into an
// object, similar to the behavior of the built-in mapper with names.
var jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.tuples(['author','format','price'])
  .map(function (tuple) {
    return { Author: tuple[0], Format: tuple[1], Price: tuple[2]};
  })
  .result()

==>

[{"Author":"John Steinbeck", 
  "Format":"audiobook", 
  "Price":16
 }, {
  "Author":"John Steinbeck", 
  "Format":"hardback", 
  "Price":20
}]
   

Comments

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