DocumentsSearch.map( mapper as objectOrFuncRef ) as DocumentsSearch
Specifies a function similar to
Array.prototype.map()
to apply to each document within the
slice or the configuration for the built-in mapper. Exclusive with
respect to the documentSearch.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. |
You cannot use this function and DocumentsSearch.reduce
in the same search. You should only call
DocumentSearch.withOptions
or
DocumentSearch.result
on the result of this operation.
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:
snippets
- Controls selection of matching text with either a boolean value or a configuration object having the
preferredMatches
,maxMatches
,perMatchTokens
, andquery
properties. For details, see Including Snippets of Matching Content in Search Results in the Search Developer's Guideextract
- Controls selection by XPath with a configuration object having the
selected
property (an enumeration ofinclude
|include-with-ancestors
|exclude
|all
) and thepaths
property. For details, see Extracting Portions of Each Matched Document in the Search Developer's Guidenamespaces
- Specifies a configuration object with prefix keys and uri values for resolving XPath expressions used in
extract
.returnSimilar
- A boolean value controlling whether to return documents similar to each result document.
A custom mapper should have the following signature:
function (currentItem)
Where currentItem is a search result descriptor object.
See DocumentsSearch.result for
an example of what this object can look like. If the mapper function
returns a value, the value is added to the results
array
or iterator.
// Use the built-in mapper to return snippets instead of documents const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.documents() .where(jsearch.byExample({synopsis: {$word: 'california'}})) .map({snippet: true}) .result()
// Use the built-in mapper to return snippets, preferrably of // matches in the "synopsis" property. Return at most 5 tokens around // the match highlight. const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.documents() .where(cts.wordQuery('california')) .map({snippet: { preferredMatches: ['synopsis'], perMatchTokens: 5 }}) .result()
// Use the built-in mapper to extract just the "title" and "author" // JSON properties from matched documents. const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.documents() .where(jsearch.byExample({synopsis: {$word: 'California'}})) .map({extract: {paths: ['/title', '/author']}}) .result()
// Use the built-in mapper to return both snippets and documents. const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.documents() .where(jsearch.byExample({synopsis: {$word: 'California'}})) .map({snippet: true, extract: {selected: 'all'}}) .result()
// Use a custom mapper to insert a new property in output document // returned for each match. const jsearch = require('/MarkLogic/jsearch.sjs'); jsearch.documents() .where(cts.jsonPropertyValueQuery('author','Mark Twain')) .map(function (value) {value.iWasHere = true; return value;}) .result() // This example produces output similar to the following, where // the "iWasHere" property was added to each result by the // custom mapper. (Some search results were elided for brevity.) // // { "results": [ // { "index": 0, // "uri": "/books/twain4.json", // "score": 14336, // "confidence": 0.43245348334312, // "fitness": 0.7490314245224, // "document": { // "title": "Adventures of Huckleberry Finn", // "author": "Mark Twain", // "edition": { // "format": "hardback", // "price": 18 // }, // "synopsis": "The adventures of Huck, a boy of 13, ..." // }, // "iWasHere": true // }, ... // ], // "estimate": 4 // }
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.