Loading TOC...

sem.sparql

sem.sparql(
   sparql as String,
   [bindings as Object?],
   [options as String[]],
   [store as sem.store[]]
) as Sequence

Summary

Executes a SPARQL query against the database.

SPARQL "SELECT" queries return a solution as a sequence of map objects in the form of a table, where each map represents a set of bindings that satisfies the query.

SPARQL "CONSTRUCT" queries return triples as a sequence of sem:triple values in an RDF graph.

SPARQL "DESCRIBE" queries return a sequence of sem:triple values as an RDF graph that describes the resources found by the query.

SPARQL "ASK" queries return a single xs:boolean value (true or false) indicating whether a query pattern matches in the dataset.

This function is a built-in.

Parameters
sparql The SPARQL query to be executed.
bindings A map containing initial values for variables from the query, or the empty sequence if no query variables are to be initially bound. This is a way to parameterize the query.
options Options as a sequence of string values. Available options are:
"base=IRI"
The initial base IRI for the query.
"default-graph=IRI*"
Add the named graph or graphs specified by the IRI to the default graph for the query.
"named-graph=IRI*"
Add the named graph or graphs specified by the IRI to the list of named graphs available in the query.
"parse-check"
Parse the query, but don't perform static checks or execute it. The default is false.
"prepare"
Parse and optimize the query, caching the result. No execution is performed. Default is false.
"optimize=N"
Sets the optimization level to use. Levels of 0 (off), 1, and 2 are recognized. The default is 1.
"trace=ID"
This parameter outputs the query's plan, optimization and execution details in the log while getting results. ID is the identifier to identify the query in the logs.
"dedup=N"
Sets de-duplication of triples results to "on" or "off". The default, standards-compliant behavior is "on".

array

Return the result as a sequence of array values (json:array).

map

Return the result as a sequence of map values, where the key is the column name (sem:binding).
store A sem:store constructor to use as the source of the triples for the SPARQL query. If multiple sem:store constructors are supplied, the triples from all the sources are merged and queried together. The default for sem:store is the current database's triple index, restricted by its options and query argument (for instance, "triples in documents matching this query").

Options for "any", "document", "properties", "locks", "checked", or "unchecked", which used to be part of the sem:sparql signature, must be specified as part of sem:store, not as part of sem:sparql. The sem:sparql query will run with the locking option specified in sem:store. Locking is ignored in a query transaction.

If a sem:store value is not supplied, then the default sem:store for the statement will be used. This is the same as calling sem:store() with all arguments omitted, which will access the current database's triple index, using the default rulesets configured for that database. The default for locking is read-write.

Required Privileges

http://marklogic.com/xdmp/privileges/sem-sparql

Usage Notes

The options parse-check and prepare cannot be used at the same time.

Example

declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");

// load an rdf triple that will match the SPARQL query :)

sem.rdfInsert(
  sem.triple(sem.iri("http://www.example.org/dept/108/invoices/20963"),
	     sem.iri("http://www.example.org/dept/108/invoices/paid"),
             "true")) ;
// returns the URI of the document that contains the triple

******

// run SPARQL against above triple

sem.sparql('\n\
PREFIX inv: <http://www.example.org/dept/108/invoices/>\n\
\n\
SELECT ?predicate ?object\n\
WHERE \n\
{ inv:20963 ?predicate ?object }\n\
');

// returns the predicate(s) and object(s) for the matching triple(s)
{
  "predicate": "http://www.example.org/dept/108/invoices/paid",
  "object": "true"
}

Example

//   this query uses the data from the previous query and shows how to
//   pass bindings in a parameter to sem:sparql

var params = {"subject":
		sem.iri("http://www.example.org/dept/108/invoices/20963")};
sem.sparql("\n\
  SELECT ?predicate ?object\n\
  WHERE\n\
  { ?subject ?predicate ?object } ",
  params);

// returns the predicate(s) and objects(s) that match the query
{
  "predicate":"<http://www.example.org/dept/108/invoices/paid>",
  "object":"true"
}

Example

declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");

// load sample data triples and ontology triple
var string ='\
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\
@prefix p0: <http://www.example.org/products/> .\
@prefix p2: <http://www.example.com/> .\
\
p2:Henley       <http://www.w3.org/2000/01/rdf-schema#subClassOf>\
                                p2:shirt .\
p0:prod:1001    p2:color        "blue" ;\
                a               p2:Henley .\
p0:prod:1002    p2:color        "blue" ;\
                a               p2:shirt .\
';
sem.rdfInsert(sem.rdfParse(string, ["graph=graph-1", "turtle"]))

// create a store that uses an RDFS ruleset for inference
var rdfsStore = sem.rulesetStore("rdfs.rules", sem.store());

// use the store you just created - pass it into sem.sparql()
var sparqlQuery = '\
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\
    prefix p0: <http://www.example.org/products/>\
    prefix p2: <http://www.example.com/>\
 \
    SELECT ?product\
    FROM <graph-1>\
    WHERE\
    {\
      ?product rdf:type p2:shirt ;\
      p2:color "blue"\
    }';
sem.sparql(
    sparqlQuery,
    [],
    [],
    rdfsStore
  )
//returns the triples that match the query
{
"product": "http://example.org/products/1001"
}
{
"product": "http://example.org/products/1002"
}

Example

declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");

// load three rdf triples, two of which will match the SPARQL query 

sem.rdfInsert(
    sem.triple(sem.iri("http://www.example.org/dept/108/invoices/20963"),
           sem.iri("http://www.example.org/dept/108/invoices/paid"),
               "true")) ;
sem.rdfInsert(
    sem.triple(sem.iri("http://www.example.org/dept/108/invoices/99887"),
            sem.iri("http://www.example.org/dept/108/invoices/paid"),
                 "true")) ;
sem.rdfInsert(
    sem.triple(sem.iri("http://www.example.org/dept/108/invoices/11111"),
            sem.iri("http://www.example.org/dept/108/invoices/paid"),
                "true")) ;
                
// returns the URI of the document that contains the triple

// Run this sparql to select two of the triples using an array of IRIs in the binding.

let binding = 
    {invoices: [
         sem.iri("http://www.example.org/dept/108/invoices/20963"),
         sem.iri("http://www.example.org/dept/108/invoices/99887")
         ]
    }
sem.sparql(`
        SELECT ?predicate ?object
        WHERE { 
            ?inv ?predicate ?object 
            FILTER (?inv = ?invoices)
        }
    `,
    binding
);

//returns the triples that match the query
{
"predicate": "http://www.example.org/dept/108/invoices/paid", 
"object": "true"
}
{
"predicate": "http://www.example.org/dept/108/invoices/paid", 
"object": "true"
}

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.