
jsearch.documents() as DocumentsSearch
Creates a DocumentsSearch object to define and execute a search for documents.
See the methods of DocumentSearch for the means of
defining search criteria (DocumentSearch.where) and
otherwise customizing your query. Search results are not generated
until you call DocumentsSearch.result.
If you do not include a call to where in your
call chain, your query matches all documents in the database.
// Find all documents where the "author" JSON property value is "Mark Twain"
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
.where(jsearch.byExample({author: 'Mark Twain'}))
.result()
/* Result: A search result that includes search metadata and matched
documents, similar to the following:
{ "results": [
{ "index": 0,
"uri": "/books/twain3.json",
"score": 16384,
"confidence": 0.43934014439583,
"fitness": 0.69645345211029,
"document": {
"title": "Adventures of Huckleberry Finn",
"author": "Mark Twain",
"edition": {
"format": "paperback",
"price": 8
},
"synopsis": "The adventures of Huck, a boy ..."
}
},
{ "index": 1,
"uri": "/books/twain1.json",
"score": 16384,
"confidence": 0.43934014439583,
"fitness": 0.69645345211029,
"document": {
"title": "Adventures of Tom Sawyer",
"author": "Mark Twain",
"edition": {
"format": "paperback",
"price": 9
},
"synopsis": "Tales of mischief and adventure ..."
}
}
],
"estimate": 2
}
*/
// Find all documents where the "synopsis" JSON property contains
// the word "california". Include snippets in the results.
const jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
.where(jsearch.byExample({synopsis: {$word: 'california'}}))
.map({snippet: true})
.result()
/* Result: A search result summary that includes search metadata and snippets
of matched content, similar to the following:
{"results":[
{"score":28672,
"fitness":0.681636929512024,
"uri":"/books/steinbeck1.json",
"path":"fn:doc(\"/books/steinbeck1.json\")",
"confidence":0.529645204544067,
"index":0,
"matches":[{
"path":"fn:doc(\"/books/steinbeck1.json\")/text(\"synopsis\")",
"matchText":[
"...from their homestead and forced to the promised land of ",
{"highlight":"California"}, "."
]
}]
},
{ ... }, ...
],
"estimate":3
}
*/
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.