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).var obj = {key1:"value1", key2:"value2"}; obj;
Click the Run button. You will see the object serialized in the results pane.
declareUpdate(); var 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(); var phrase1 = "The quick brown fox jumps over the lazy dog."; var fox = {fox: phrase1}; var phrase2 = "The huge white elephant walked in the mud."; var elephant = {elephant: phrase2}; var phrase3 = "The fast electric car drove down the highway."; var car = { car: phrase3}; var 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"]} }; var col = "my-phrases"; var 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 var count = 0; var results = new Array(); for (var 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 var count = 0; var results = new Array(); for (var 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 var count = 0; var results = new Array(); for (var result of fn.collection() ) { var res = result.root.car; var 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:var jsearch = require('/MarkLogic/jsearch.sjs'); 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.