Loading TOC...

FacetDefinition.map

FacetDefinition.map(
   mapper as objectOrFuncRef
) as FacetDefinition

Summary

Specifies a function similar to Array.prototype.map() to apply to each value within the slice or the configuration for the built-in mapper, as in a ValuesSearch definition.

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 index value to act on. For example, a string if generating facets on a string-typed lexicon or index. If the function returns a value, the value is added to the results array or iterator.

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

See Also

Example


// Use the built-in mapper to suppress the inclusion of counts 
// in returned facet data.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.facets(
    jsearch.facet('Author', 'author').map({frequency: 'none'}))
  .where(cts.directoryQuery('/books/'))
  .result()

// Produces results similar to the following:
// {"facets": {
//   "Author": [
//     "John Steinbeck", 
//     "Mark Twain", 
//     "Robert Frost"
//   ]
// }}
   

Example


// Use a custom mapper over a string-typed index to convert the
// generated facet values to lower case.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.facets(
    jsearch.facet('Author', 'author')
           .map(function (currentItem) {
                  return fn.lowerCase(currentItem)
                }
            ))
  .where(cts.directoryQuery('/books/'))
  .result()

// Produces results similar to the following:
// {"facets": {
//   "Author": [
//     "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.