This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This could be any query. Here we've simplified to a single word query. | |
var query = "flannel"; | |
var results = []; // Accumulator for result snippets | |
for(var doc of cts.search(query)) { | |
var builder = new NodeBuilder(); | |
// The callback is the function that's executed when highlight finds a match | |
// to the query | |
function callback(builder, text, node, queries, start) { | |
builder.addNode({"match": text}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function highlight(node, query) { | |
var match = 0; | |
var threshold = 2; | |
// NodeBuilder is used to "inject" highlights into the flow of the matched nodes. | |
var builder = new NodeBuilder(); | |
function callback(builder, text, node, queries, start) { | |
// Keep track of the number of matches | |
match++; | |
// Inject a new element in context to highlight the match | |
builder.startElement("b") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Evaluate standard XPath against JSON (or XML or both). JSON properties operate similar to unnamespaced | |
* XML elements. | |
*/ | |
var total = 0, count = 0; | |
// XPath is a subset of XQuery. Evaluate it. | |
var resultIter = | |
xdmp.xqueryEval('/node()[some $name in friends/name satisfies starts-with(upper-case($name), "N") or eyeColor = "blue"]'); | |
for (var result of resultIter) { | |
total += result.age; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nb = new NodeBuilder(); | |
nb.startDocument() | |
.startElement('ex:article', 'http://example.com') | |
.addAttribute('is-draft','true'); | |
for (var i = 0; i < 17; i++) { | |
nb.startElement('ex:p', 'http://example.com') | |
.addText('This is paragraph ') + | |
nb.startElement('em', '') // No namespace | |
.addText((i + 1).toString()) | |
.endElement() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkStatus() { | |
// look up the set of databases this user is allowed to work with | |
var myDatabaseIds = ...; | |
if (myDatabaseIds.length > 0) { | |
return xdmp.databaseBackupStatus(myDatabaseIds; | |
} | |
} | |
// module.amp takes a function as input and returns a function | |
// that has the same signature as the original function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var status = require("status"); | |
// The calling module does not need to know that an imported function is amped. | |
// This is controlled by the combination of configuration and the library | |
// module's export declaration. | |
status.checkStatus(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var admin = require("/MarkLogic/admin"); | |
// Use the (XQuery) Admin APIs to create some indexes. | |
var config = admin.getConfiguration(); | |
try { | |
// Create a dateTime index on the registered property. For the purposes of | |
// creating range indexes a JSON property is the same as an XML element (i.e. | |
// this will also add XML elements named "registered" in no namespace to this | |
// index.) Since JSON lacks a native Date type (though JavaScript has one), | |
// the indexer automatically parses strings that are formatted as ISO8601 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
// Export the "private" function with a public name | |
parseCurrency: dollarToObject | |
}; | |
// "Private" function local to the module | |
function dollarToObject(str) { | |
if (str && str.match(/^\s*\$/)) { | |
return { | |
"value": parseFloat(str.replace(/[$,]/g, "")), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the declared exports from the "util" module under the util "namespace". | |
// Because the path does not start with a / (or C:\ on Windows), resolve | |
// the module path relative to the current module. | |
// Because there is no file extension, first try .sjs then .xqy. | |
// In this case, it resolves to ./util.sjs. | |
var util = require("util"); | |
// Call the imported function | |
util.parseCurrency("$44,3829.01"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tell the transaction manager that we're about to make an update | |
declareUpdate(); | |
// For each item in the collection, update the balance field from a string | |
// balance: "$1,234.56" | |
// to a Number representing the value and a unit of measure | |
// balance: { value: 1234.56, unit: "USD" } | |
for (var item of fn.collection("fake data")) { | |
// Turn the JSON node from the database into a JavaScript object so we can | |
// update it. JSON nodes are immutable. |