Skip to content

Instantly share code, notes, and snippets.

View dmcassel's full-sized avatar

David Cassel dmcassel

View GitHub Profile
@dmcassel
dmcassel / configure-db.sh
Last active September 23, 2015 20:16
REST API Setup steps
curl -v -X PUT --anyauth -u admin:admin \
--header "Content-Type:application/json" \
-d '{"collection-lexicon":true}' \
http://localhost:8002/manage/v2/databases/TutorialDB/properties
@dmcassel
dmcassel / ex01.sh
Last active September 23, 2015 13:29 — forked from evanlenz/ex01.sh
Code samples for MarkLogic REST API
curl -X GET \
--anyauth --user rest-writer:x \
'http://localhost:8011/v1/search?q=chicken&format=json'
xdmp.setResponseContentType("application/json");
switch(xdmp.getRequestMethod()) {
case "GET":
// Get the criterion out of the URL query string
var q = cts.wordQuery(xdmp.getRequestField("q"));
// Unwind the iterator into an Array
var results = cts.search(q).toArray();
xdmp.addResponseHeader("X-Result-Count", results.length + "");
// Serialize the JavaScript results object as JSON
(:
: Evaluate JavaScript from XQuery, passing in external variables and returning
: results as XQuery types.
:)
xdmp:javascript-eval(
"var result = []; for(var p in param) {result.push(p); result.push(param[p]);} result;",
(xs:QName("param"), object-node { "a": "A", "b": "B"})
)
/*
* Access the values from a range index.
* (Note: This assumes you've created the path range index on the
* balance.value field in "Import XQuery")
*/
cts.values(
// Any reference to a range index
cts.pathReference("balance/value")
)
/*
* Demonstrate an object-oriented style of programming. Declare a Person type
* and instantiate an instance. Convert that instance into a JSON node, for
* example to persist it to the database, and convert it back.
*/
// The Person constructor, e.g. var p = new Person(…)
Person = function(fname, lname) {
if (fname) this.fname = fname;
if (lname) this.lname = lname;
[
{
"guid": "986af6e1-e0f1-450f-b1f0-2eff54357840",
"about": [
"Skateboard pop-up kogi, ethnic Vice disrupt Truffaut twee fashion axe forage occupy biodiesel. Bespoke umami yr, ",
{
"match": "flannel"
},
" kogi XOXO bitters butcher ugh DIY lomo. Flexitarian distillery ",
{
// 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});
/*
* 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;
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()