This section describes the following procedures to get started creating and querying documents using JavaScript in MarkLogic:
Query Console is an interactive web-based query tool that is bundled with MarkLogic. Query Console enables you to write ad-hoc queries and view the results without using .sjs
or .xqy
files. You can view the results as formatted (Auto) or in plain-text (Raw) output. Query Console is designed for a modern web browser with JavaScript support.
Many of the examples in this document assume you are using Query Console. To learn more about Query Console, see the Query Console Walkthrough in the Query Console User Guide
This section walks you through creating some simple documents in JavaScript and contains the following parts:
To create a simple document and query it, perform the following steps:
admin
role (you can use the same user created when you installed MarkLogic).const obj = {key1:"value1", key2:"value2"}; obj;
Click the Run button. You will see the object serialized in the results pane.
declareUpdate(); const obj = {key1:"value1", key2:"value2"}; xdmp.documentInsert("/simple.json", obj);
Click the Run button. You will see your query response was empty in the results pane. But what actually happened is you created a document in the database. The declareUpdate()
function is required everytime your program includes an update to the database (otherwise an exception is thrown--try it).
cts.doc("/simple.json");
Click the Run button. You will see the JSON document you created in the results pane.
key1
property, enter the following and then click the Run button:cts.doc("/simple.json").root.key1;
You will see the value for the key1
property (value1
) in the results pane. Note that cts.doc returns a document node, so you first have to navigate to the root
to get to the JSON node, and then you can navigate to the key1
property.
To create some documents and then perform some searches against them, perform the following steps:
// create some documents to search over declareUpdate(); const phrase1 = "The quick brown fox jumps over the lazy dog."; const fox = {fox: phrase1}; const phrase2 = "The huge white elephant walked in the mud."; const elephant = {elephant: phrase2}; const phrase3 = "The fast electric car drove down the highway."; const car = { car: phrase3}; const obj1 = { fox: {colors:["brown", "red", "yellow"], actions:["jump", "sleep", "eat"]} , elephant: {colors:["brown", "white", "green"], actions:["blow horn", "sleep", "eat", "walk"]}, car: {colors:["brown", "red", "yellow", "green", "grey"], actions:["speed", "drive", "stop", "accelerate"]} }; const col = "my-phrases"; const perms = [xdmp.permission("qconsole-user", "read"), xdmp.permission("qconsole-user", "update")]; xdmp.documentInsert("/fox.json", fox, perms, col); xdmp.documentInsert("/elephant.json", elephant, perms, col); xdmp.documentInsert("/car.json", car, perms, col); xdmp.documentInsert("/stuff.json", obj1, perms, col);
// find all documents with the word "the" in the "my-phrases" // collection and count them let count = 0; const results = new Array(); for (const result of cts.search( cts.andQuery(["the", cts.collectionQuery("my-phrases")]))) { count++; results.push(result); }; results.push(fn.concat("Count = ", count)); results;
This returns the following array:
[ {"car":"The fast electric car drove down the highway."}, {"fox":"The quick brown fox jumps over the lazy dog."}, {"elephant":"The huge white elephant walked in the mud."}, "Count = 3" ]
// find all documents in the "my-phrases" collection // with the word "car" and count them let count = 0; const results = new Array(); for (const result of cts.search(cts.andQuery([ cts.collectionQuery("my-phrases"), cts.wordQuery("car")])) ) { count++; results.push(result); }; results.push(fn.concat("Count = ", count)); results;
This returns the following array:
[ {"car":"The fast electric car drove down the highway."}, "Count = 1" ]
// find all documents with the word "car" and count them let count = 0; const results = new Array(); for (const result of fn.collection() ) { let res = result.root.car; let x = res; count++; results.push(x); }; results.push(fn.concat("Count = ", count)); results;
This returns the following array:
[ "The fast electric car drove down the highway.", null, null, {"colors":["brown", "red", "yellow", "green", "grey"], "actions":["speed", "drive", "stop", "accelerate"]}, "Count = 4" ]
cts.estimate("car");
This is an estimate of the number of documents that match the query, which means it is the number of documents that the indexes indicate are a match. For many searches, this will be accurate. For details about search, see the Search Developer's Guide.
jsearch
API to perform the search as follows:import * as jsearch from '/MarkLogic/jsearch.mjs'; jsearch.documents() .where([cts.collectionQuery('my-phrases'), cts.wordQuery('car')]) .map({snippet: true}) .result();
This returns two objects, the first is the HTTP header and the second contains the response from the endpoint, which contains a report detailing what matches the search.
{"results": [{"score":108544, "fitness":0.670031011104584, "uri":"/car.json", "path":"fn:doc(\"/car.json\")", "confidence":0.497606456279755, "index":0, "matches": [{"path":"fn:doc(\"/car.json\")/text(\"car\")", "matchText": ["The fast electric ", {"highlight":"car"}, " drove down the highway."] }] }], "estimate":1 }
For details about jsearch
, see jsearch API Documentation and Creating JavaScript Search Applications in the Search Developer's Guide.
This is just a very brief introduction to how you can search in documents. For information about the search functionality possible in MarkLogic, see the Search Developer's Guide.
This section walks you through some Javascript changes that are implemented with a view towards improving Javascript performance:
Evaluating a JavaScript program may generate side-effects on the JavaScript global environment; therefore, each JavaScript program is evaluated in a separate v8 context. The overhead of creating such a context is significant, and in the recent v8 version that overhead has increased by roughly 40%.
To compensate for this overhead, it is suggested that you convert your Javascript scripts to Javascript modules. A Javascript module program is one that adheres to the following:
For further reading on the details of converting script programs into module programs, please see our Javascript Reference Guide.
In order to support Javascript module programs, a new server mimetype has been created. All module URIs must conform to the new mimetype:
You may view this new mimetype by navigating to the Admin UI and selecting the Mimetypes from the explorer pane.
The extension for a module URI in the import statement may be omitted. When the module URI in an import statement doesn't contain an extension, an extension mapping to any of the above MIME types must be added to resolve the specified module. For example:
import { square, diag } from 'lib/top'; // map to lib/top.js or lib/top.mjs