Skip to content

Instantly share code, notes, and snippets.

View ableasdale's full-sized avatar

Alex Bleasdale ableasdale

  • Cyera
  • Exeter
View GitHub Profile
@ableasdale
ableasdale / backup-status.xqy
Last active April 26, 2016 18:09
MarkLogic: Get backup (job) status via ReST call given an id
xquery version "1.0-ml";
(: See http://docs.marklogic.com/REST/POST/manage/v2/databases/%5Bid-or-name%5D :)
declare variable $payload-status := '{"operation": "backup-status", "job-id" : "' || "12979282468857286095" || '","host-name": "' || xdmp:host-name() || '"}';
xdmp:http-post("http://localhost:8002/manage/v2/databases/Documents?format=json",
<options xmlns="xdmp:http">
<authentication method="digest">
<username>username</username>
@ableasdale
ableasdale / rest-backup-forest.xqy
Last active April 26, 2016 18:09
MarkLogic: Create a backup using a ReST API call
xquery version "1.0-ml";
(: See http://docs.marklogic.com/REST/POST/manage/v2/databases/%5Bid-or-name%5D :)
declare variable $payload-status := '{"operation": "backup-database", "forest": ["Documents"], "backup-dir": "/tmp" }';
xdmp:http-post("http://localhost:8002/manage/v2/databases/Documents?format=json",
<options xmlns="xdmp:http">
<authentication method="digest">
<username>username</username>
@ableasdale
ableasdale / database-reorder-forests.xqy
Created February 15, 2016 08:58
Reordering forests for the rebalancer after adding more hosts to a cluster.
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare variable $DATABASE as xs:string := "example-db";
let $config := admin:get-configuration()
let $dbid := admin:database-get-id($config, $DATABASE)
return
@ableasdale
ableasdale / sample-csv.xqy
Created February 17, 2016 10:13
Sample CSV output
xquery version "1.0-ml";
declare namespace obj="http://marklogic.com/solutions/obi/object";
declare function local:header(){
text {"One,Two"}
};
declare function local:process($uri as xs:string) {
text {fn:string-join((cts:element-values(xs:QName("obj:id"), (), (), cts:document-query($uri)), cts:element-values(xs:QName("obj:type"), (), (), cts:document-query($uri))), ",")}
@ableasdale
ableasdale / bootstrap-tmpl.xqy
Created February 17, 2016 14:19
MarkLogic Bootstrap Page Template
xquery version "1.0-ml";
declare function local:create-bootstrap-page($title as xs:string, $content as element()){
element html { attribute lang {"en"},
element head {
element meta { attribute charset {"utf-8"}},
element meta { attribute http-equiv {"X-UA-Compatible"}, attribute content {"IE=edge"} },
element meta { attribute name {"viewport"}, attribute content {"width=device-width, initial-scale=1"} },
element title {$title},
element link {
@ableasdale
ableasdale / jsearch.sjs
Created February 23, 2016 10:30
MarkLogic JSearch example
var jsearch = require('/MarkLogic/jsearch.sjs');
jsearch.documents()
.where( jsearch.byExample({ orderDate: {$ge: "1960-01-01T00:00:00.000"} }) )
.orderBy(cts.indexOrder(cts.jsonPropertyReference('orderDate'), 'ascending'))
.slice(0,10)
.map(function(doc) {return doc.document;})
.withOptions({returnQueryPlay: true})
.result();
@ableasdale
ableasdale / forest-and-replica-rest.xqy
Last active February 24, 2016 15:16
MarkLogic: ReST API - Create a forest with a replica on another host
xquery version "1.0-ml";
(:
1) setup a cluster with a couple of hosts (127.0.0.1, 127.0.0.2)
2) create forest (replicatest) on host 127.0.0.2
3) create forest (test) on host 127.0.0.1
:)
declare variable $payload-status := '{
"forest-name":"test",
"host":"' || xdmp:hostname() || '",
@ableasdale
ableasdale / sar-switches.bat
Created February 29, 2016 11:21
Example SAR switches
sar -uqBrp -n ALL -f 04_sa20
@ableasdale
ableasdale / binary-xdmp-plan.xqy
Created March 18, 2016 09:41
Useful xdmp:plan for binary
declare namespace qry = "http://marklogic.com/cts/query"; cts:uris((),(),cts:and-query(xdmp:plan(/binary())//qry:term-query/qry:key ! cts:term-query(.)))
@ableasdale
ableasdale / timestamps.xqy
Created March 24, 2016 17:02
Get Timestamps from the log and get times for them
(: 2016-03-21 19:36:59.684 Notice: Documents: XDMP-OLDSTAMP: Timestamp too old for forest Documents (14582829718819729 < 14584613478666680; 14585890140870470) :)
(14582829718819729, 14584613478666680, 14585890140870470) ! xdmp:timestamp-to-wallclock(.)