Skip to content

Instantly share code, notes, and snippets.

View ableasdale's full-sized avatar

Alex Bleasdale ableasdale

View GitHub Profile
@ableasdale
ableasdale / unserialize.js
Last active July 11, 2016 11:05
MarkLogic / JavaScript: Unserializing output from PHP's serialize()
/**
* Unserialize data taken from PHP's serialize() output
*
* Taken from https://github.com/kvz/phpjs/blob/master/functions/var/unserialize.js
* Fixed window reference to make it nodejs-compatible
*
* @param string serialized data
* @return unserialized data
* @throws
*/
@ableasdale
ableasdale / marklogic-numeric-locale.conf
Created July 6, 2016 10:46
MarkLogic - fixing locale issue where XDMP-LEXVAL: ss:request-rate is thrown
export LC_NUMERIC=en_US.UTF-8
@ableasdale
ableasdale / durations.xqy
Created July 4, 2016 13:22
MarkLogic / XQuery: dealing with date (duration) calculations
xquery version "1.0-ml";
declare variable $dt as xs:dateTime := fn:current-dateTime();
element data {
element current {$dt},
element current-minus-seven-days {$dt - xs:dayTimeDuration('P7D')},
element current-plus-seven-days {$dt + xs:dayTimeDuration('P7D')},
element current-plus-one-hour {$dt + xs:dayTimeDuration('PT1H')}
}
@ableasdale
ableasdale / sparql-binding.sparql
Created June 14, 2016 12:18
MarkLogic and SPARQL binding to the XPath function namespace allows you to do things like...
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
select ?x where
{
BIND (fn:doc("/matches/2010_FIFA_WORLD_CUP_special_1.xml") as ?x)
}
@ableasdale
ableasdale / relative-to-absolute.xqy
Created May 27, 2016 16:46
Patching relative links in an XSD
xquery version "1.0-ml";
declare function local:current-directory($uri as xs:string) as xs:string
{
fn:string-join(
let $parts := fn:tokenize($uri, "/")
let $count := fn:count($parts) - 1
return $parts[1 to $count]
, "/"
) || "/"
@ableasdale
ableasdale / reorder-forests.xqy
Last active May 26, 2016 16:15
Reorder forests and save the configuration back to MarkLogic
xquery version "1.0-ml";
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $dbid := admin:database-get-id($config, "test-db")
let $forests := admin:database-get-attached-forests($config,$dbid)
let $config := admin:database-reorder-forests($config, $dbid, ( (Forest1, Forest2,.. ))
@ableasdale
ableasdale / get-forests-in-order.xqy
Created May 26, 2016 16:13
Gets an ordered list of forests for a given database
xquery version "1.0-ml";
(: Returns a list of forests in order for a given database :)
import module namespace admin = "http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
let $config := admin:get-configuration()
let $dbid := admin:database-get-id($config, "test-db")
return admin:database-get-attached-forests($config,$dbid) ! xdmp:forest-name(.)
@ableasdale
ableasdale / marklogic-stopwords.xqy
Created May 19, 2016 07:32
Simple stopwords example
xquery version "1.0-ml";
(: imagine this is a string from an input field :)
declare variable $STRING as xs:string := "the quick brown fox jumped over the lazy dog";
(: this would be a comprehensive list of words they want to exclude from any searches to improve relevance/scoring etc :)
declare variable $STOPWORD-LIST as xs:string+ := ("the");
let $searchable-tokens := for $token in fn:tokenize($STRING, " ")
return if(some $word in $STOPWORD-LIST satisfies ($token eq $word))
@ableasdale
ableasdale / save-sparql-as-csv-from-xquery.xqy
Created March 30, 2016 14:25
Getting a SPARQL ReST endpoint to return csv data
xquery version "1.0-ml";
declare variable $HOSTNAME := "yourhostname";
declare variable $DATABASE := "Documents";
declare variable $USERNAME := "user";
declare variable $PASSWORD := "pass";
declare variable $SPARQL-QUERY := 'SELECT DISTINCT * WHERE {?s ?p ?o} LIMIT 10';
let $response := xdmp:http-post("http://"||$HOSTNAME||":8000/v1/graphs/sparql?database="||$DATABASE,
<options xmlns="xdmp:http">
@ableasdale
ableasdale / curl-sparql.sh
Created March 30, 2016 14:07
Using cURL to run a simple query against a database containing triples
curl --anyauth --user user:pass "http://hostname:8000/v1/graphs/sparql?database=Documents" -H "Content-type:application/x-www-form-urlencoded" -H "Accept:text/csv" -X POST --data-binary 'query=SELECT+*+WHERE+{+%3fs+%3fp+%3fo+}+LIMIT+10'