Loading TOC...

GET /v1/graphs/sparql


Perform a SPARQL query on the database.

URL Parameters
query A URL-encoded SPARQL query operation.
default-graph-uri* The URI of the graph or graphs to use as the default graph.
named-graph-uri* The URI of a named graph or graphs to include in the query operation.
database? Perform this operation on the named content database instead of the default content database associated with the REST API instance. Using an alternative database requires the "eval-in" privilege; for details, see Security Requirements in the REST Application Developer's Guide.
base? The initial base IRI for the query.
txid? The transaction identifier of the multi-statement transaction in which to service this request. Use the /transactions service to create and manage multi-statement transactions.
start? The index of the first result to return. Results are numbered beginning with 1. Default: 1. This parameter only applies when evaluating a SPARQL SELECT query.
pageLength? The maximum number of results to return in this request. By default, all results are returned. This parameter is only applies when evaluating a SPARQL SELECT query.
q? A string query. For details on the syntax, see Searching Using String Queries in the Search Developer's Guide and search:search.
structuredQuery? A structured search query string. That is, a serialized representation of a search:query element. See Searching Using Structured Queries in the Search Developer's Guide.
options? The name of query options previously created via a PUT or POST request to the /v1/config/query service.
collection* Filter results to include only matches in the named collection(s).
directory? Filter results to include only matches from documents in the specified database directory.
ruleset* The name of rulesets to include for inferring triples. Ruleset names can be those of the built-in rulesets, or user-managed rulesets stored in the schemas database.
default-rulesets? A flag whether to include database-default inference or not. Valid values are include (default) or exclude.
optimize? A number indicating how much time for the query engine to spend analyzing a query. (See sem:sparql.)
bind:{name}* A binding name and value. This format assumes that the type of the bind variable is an IRI.
bind:{name}:{type}* A binding name, type, and value. This parameter format accepts any XSD type, or a two-character language code.
bind:{name}@{lang}* A binding name, type, and value. Use this pattern to bind to language-tagged strings.
Request Headers
Accept* The expected MIME type of the response. The response MIME type depends on the type of information generated by the query. For details, see Response Output Formats in the Semantics Developer's Guide.
Response Headers
Content-type The MIME type of the data in the response body when performing a query. The MIME type depends on the type of information generated by the query. For details, see Response Output Formats in the Semantics Developer's Guide.


Upon success, MarkLogic Server returns status 200 (OK) and the response body contains the query result. If your request results in an unsupported format, MarkLogic Server returns status 406 (Not Acceptable).

Required Privileges

This operation requires the rest-reader role, or the following privilege:


Usage Notes

This interface implements the GET method of the W3C SPARQL Protocol; for details, see http://www.w3.org/TR/2013/REC-sparql11-protocol-20130321#query-operation.

You can specify the RDF Dataset against which to execute the query using the named-graph-uri and default-graph-uri request parameters, or within the query itself. If the dataset is specified in both the request parameters and the query, the dataset defined by the request parameters is used. If neither is specified, all graphs are included in the operation.

If the graph(s) you query use embedded (unmanaged) triples, you can constrain your SPARQL query results to triples occurring in documents that match document search criteria defined by the following parameters: q, structuredQuery, and options. Applying document search criteria to graphs containing managed triples is not recommended because it can yield unexpected results when the search matches the internal documents in which triples are stored.

The collection lexicon must be enabled on the database before using using the GRAPH "?g" construct in SPARQL queries. You can enable the collection lexicon from the Admin Interface.

The response MIME type depends on the type of SPARQL query and the Accept header MIME type. The four types of SPARQL query ( SELECT, CONSTRUCT, DESCRIBE, ASK) return data in different formats. For details, see Response Output Formats in the Semantics Developer's Guide.

See Also


 => This request uses the encoded version of this SPARQL query:
   SELECT ?s 
   WHERE {
$ curl --anyauth --user user:password -i -X GET \
    -H "Accept: application/sparql-results+json" \

==> Run the SPARQL query passed as the URL encoded value of the
    query request parameter. MarkLogic Server responds with output
    similar to the following:

HTTP/1.1 200 OK
Content-type: application/sparql-results+json
Server: MarkLogic
Content-Length: 126
Connection: Keep-Alive
Keep-Alive: timeout=5

  "head": {
    "vars": [ "s" ]
  "results": {
    "bindings": [ {
      "s": {
        "type": "uri",
        "value": "http://dbpedia.org/resource/Alfonso_Arau"
    } ]

If you set the Accept header to application/sparql-results+xml, 
MarkLogic Server returns data similar to the following:

<sparql xmlns="http://www.w3.org/2005/sparql-results#">
    <variable name="s"/>
      <binding name="s">


  • Perhaps I'm doing something wrong, but when I try to use "application/sparql-results+json" in the accept parameter, I get an error: let $query := xdmp:url-encode(' prefix foaf: <http://xmlns.com/foaf/0.1/> construct { ?topic ?p ?o } where { ?topic foaf:name "Zorba the Greek"@en . ?topic ?p ?o . } ') return xdmp:http-get(fn:concat("http://host:port/v1/graphs/sparql?query=",$query), <options xmlns="xdmp:http"> <headers> <accept>application/sparql-results+json</accept> </headers> </options>) ==> <response xmlns="xdmp:http"> <code>406</code> <message>Unacceptable Type</message> <headers> <content-type>application/json</content-type> <server>MarkLogic</server> <content-length>257</content-length> <connection>Keep-Alive</connection> <keep-alive>timeout=5</keep-alive> </headers> </response> Note that it seems to give back n-quads if I don't specify anything.
    • Hi David, This is a CONSTRUCT query, which returns RDF rather than sparql results. SPARQL results format is for a SELECT query, and is basically a table of results. here, try accept: application/rdf+json if you'd like to get back rdf/json. I also think you'll need to change the prefix declaration... something weird happened there. Try prefix: <http://xmlns.com/foaf/0.1/>
      • Thanks Charles! That explains it. I don't know what happened to the prefix, but it's from the cut and paste with this comment tool - which you can tell since it happened to you too... :-) As a note, I'm trying to figure this out because I'm not sure the ajax call in the infopanel tutorial: http://developer.marklogic.com/learn/semantic-infopanel is working. I'm assuming that what I've done with xdmp:http-get is the same as the following, so let me know if you think I'm missing something in my http-get request: $. ajax({ url: "/v1/graphs/sparql", accepts: { json:"application/rdf+json" }, dataType: "json", data: {query: 'prefix foaf: <http://xmlns.com/foaf/0.1/> ' + 'construct { ?topic ?p ?o } ' + 'where ' + '{ ?topic foaf:name "' + 'Zorba the Greek' + '"@en . ' + '?topic ?p ?o . }' },
Powered by MarkLogic Server 7.0-4.1 and rundmc | Terms of Use | Privacy Policy