TuplesSearch.map( mapper as objectOrFuncRef ) as TuplesSearch
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. |
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 currentItem is the current tuple to act on, in the
form of an array containing an item representing a value from each
index over which you generate co-occurrences. For example, if you
specify 3 index in your call to jsearch.tuples
then
currentItem is an array of length 3. If the mapper function
returns a value, the value is added to the results
array
or iterator.
You cannot use this method in conjunction with
TuplesSearch.reduce
.
// Configure the built-in mapper to return frequency, and specify // names for each value in the tuple const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.tuples(['author','format','price']) .slice(0,2) .map({frequency: 'item', names: ['Author','Format','Price','Freq']}) .result() /* 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 }] */
// Supply a custom mapper. This one simply recasts the tuple into an // object, similar to the behavior of the built-in mapper with names. const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.tuples(['author','format','price']) .map(function (tuple) { return { Author: tuple[0], Format: tuple[1], Price: tuple[2]}; }) .result() /* Result: [{"Author":"John Steinbeck", "Format":"audiobook", "Price":16 }, { "Author":"John Steinbeck", "Format":"hardback", "Price":20 }] */
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.