Loading TOC...

ValuesSearch.map

ValuesSearch.map(
   mapper as objectOrFuncRef
) as ValuesSearch

Summary

Specify a function similar to Array.prototype.map() to apply to each value within the slice, or configure the built-in mapper. Exclusive with respect to the reduce() clause.

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 or your custom mapper is applied to each value in the current slice of values from a lexicon or range index.

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 values, the number of occurrences of each value (that is, the items), or to omit the frequency. The default is "none".
names
Specify a name for the values and for the frequency (if enabled). If names is specified, the built-in mapper returns an object for each value with the names as properties. If the frequency is specified but not the name, the built-in mapper returns an array for each value. The frequency name should appear after the value name. For example: {names: [valName, freqName]}. See the example, below.

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

A custom mapper should have the following signature:

function (currentItem)

Where currentItem is the current value to act on. If the function returns a value, the value is added to the results array or iterator.

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

See Also

Example


// Built-in mapper configure to return frequency with the values
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.values('author')
  .map({frequency: 'item'})
  .result();

// Result: [["John Steinbeck", 3], ["Mark Twain", 4], ["Robert Frost", 1]]
   

Example


// Built-in mapper configured with frequency and names
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.values('author')
  .map({frequency: 'item', names: ['Author', 'Count']})
  .result();

/* Result:
[{"Author":"John Steinbeck", "Count":3}, 
 {"Author":"Mark Twain", "Count":4}, 
 {"Author":"Robert Frost", "Count":1}]
*/
   

Example


// Use a custom mapper
const jsearch = require('/MarkLogic/jsearch.sjs');
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.values('author')
  .map(function (value) {
     return String(value).toLowerCase() 
   })
  .result();

// Result: ["john steinbeck", "mark twain", "robert frost"]
   

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