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 / load-trusted-pem-certificate.xqy
Created January 28, 2016 12:17
Using Query Console to load a trusted certificate into MarkLogic Server
xquery version "1.0-ml";
import module namespace pki = "http://marklogic.com/xdmp/pki" at "/MarkLogic/pki.xqy";
pki:insert-trusted-certificates(
xdmp:document-get("/OurCertificateLocation/DemoLabCA.pem",
<options xmlns="xdmp:document-get">
<format>text</format>
</options>)
)
@ableasdale
ableasdale / term-keys.xqy
Created December 30, 2015 09:13
Get unique term keys from xdmp:plan
xquery version "1.0-ml";
declare namespace qry="http://marklogic.com/cts/query";
declare variable $QUERY as cts:query := cts:word-query( ("one", "two") );
declare function local:get-keys($query as cts:query) as xs:unsignedLong* {
fn:distinct-values(fn:data(xdmp:plan( cts:search(doc(), $QUERY ) )//qry:key))
};
@ableasdale
ableasdale / group-level-caches.xqy
Created December 29, 2015 23:40
Group Level Cache settings template
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
declare variable $SIXTEENTH as xs:unsignedInt := 1024;
declare variable $EIGHTH as xs:unsignedInt := 2048;
declare variable $PARTITIONS as xs:unsignedInt := 1;
let $config := admin:get-configuration()
let $groupid := admin:group-get-id($config, "Default")
@ableasdale
ableasdale / security-users-and-perms.xqy
Created December 23, 2015 12:13
Generic template for creating MarkLogic Users, Privileges and Roles
xquery version "1.0-ml";
(:~
: Security Module for creating the Generic Database Users and roles
:
: @version 1.0
:)
import module namespace sec = "http://marklogic.com/xdmp/security" at "/MarkLogic/security.xqy";
@ableasdale
ableasdale / tokenize.xqy
Created December 21, 2015 08:35
Tokenize a large file of individual strings (line by line) and create an XQuery sequence
xquery version "1.0-ml";
(: Take the 3esl.txt file and create a sequence of words :)
declare variable $file := xdmp:document-get("https://path-to-your/3esl.txt");
let $words := for $i in fn:tokenize($file,"\r?\n")
return concat('"',$i, '"')
return string-join($words,",")
@ableasdale
ableasdale / first-cut.xcc.cs
Last active October 14, 2016 22:45
Attempt to write a multi statement ad-hoc query in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Marklogic.Xcc;
using Marklogic.Xcc.Exceptions;
namespace ConsoleApplication1
{
@ableasdale
ableasdale / server-side-map.xqy
Created December 5, 2015 17:02
Pattern for storing contents of a query in a server-side map in MarkLogic Server
xquery version "1.0-ml";
declare function local:get-map-by-name($name) {
if (
fn:exists(xdmp:get-server-field($name))
) then (
xdmp:log(fn:concat("A server-side map with the following name (", $name, ") exists - returning it")), xdmp:get-server-field($name)
) else (
xdmp:log(fn:concat("No server-side map was found with the name (", $name, ") - creating it")), xdmp:set-server-field($name, map:map())
)
@ableasdale
ableasdale / test-data-generator.xqy
Created December 5, 2015 16:11
Simplistic test data XQuery stub
xquery version "1.0-ml";
for $x in (1 to 20)
return
xdmp:spawn-function(function()
{
for $i at $pos in 1 to 50000
return xdmp:document-insert(
fn:concat("/", xdmp:random(), ".xml"),
@ableasdale
ableasdale / expunge-locks.xqy
Last active November 24, 2015 18:34
Set "expunge locks" to "none" for a given database
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
(: Configure the $DATABASE variable in the line below :)
declare variable $DATABASE as xs:string := "Documents";
admin:save-configuration(admin:database-set-expunge-locks(admin:get-configuration(), xdmp:database($DATABASE), "none"))
@ableasdale
ableasdale / customer-search.kql
Created November 23, 2015 19:14
KQL Select example for customer tickets
SELECT 'Tickets.Ticket ID', 'Tickets.Priority', 'Tickets.Subject', 'Tickets.Creation Date', 'Tickets.Resolved Date', 'Tickets.User', 'Tickets.Owner', 'Tickets.Status' WHERE 'Users.User Organization' = 'MarkLogic' AND 'Tickets.Creation Date' = MONTHRANGE(October 2014, November 2015) ORDER BY 'Tickets.Creation Date'