Loading TOC...

GET /v1/graphs/sparql

Summary

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.
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.
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.
timestamp? A timestamp returned in the ML-Effective-Timestamp header of a previous request. Use this parameter to fetch SPARQL query results based the database contents at a fixed point-in-time. For more details, see Performing Point-in-Time Operations in the REST Application Developer's Guide. You cannot use this parameter with a SPARQL update.
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.
ML-Effective-Timestamp The system timestamp at which this operation was performed. You can use the value in the timestamp parameter of a subsequent request. For more details, see Performing Point-in-Time Operations in the REST Application Developer's Guide.

Response

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:

http://marklogic.com/xdmp/privileges/rest-reader

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 when using using the GRAPH "?g" construct in SPARQL queries. You can check and enable this setting 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

Example

 => This request uses the encoded version of this SPARQL query:
   SELECT ?s 
   WHERE {
   ?s 
   <http://dbpedia.org/ontology/director> 
   <http://dbpedia.org/resource/Mexico> 
   }
 
 
$ curl --anyauth --user user:password -i -X GET \
    -H "Accept: application/sparql-results+json" \
	http://localhost:8000/v1/graphs/sparql?query="\
	SELECT%20%3Fs%20%0AWHERE%20%7B%0A%20%20%3Fs%20%0A%20%20%3C\
	http%3A%2F%2Fdbpedia.org%2Fontology%2Fdirector%3E%20%0A%20\
	%20%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FMexico%3E%20%0A%7D"

==> 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#">
  <head>
    <variable name="s"/>
  </head>
  <results>
    <result>
      <binding name="s">
        <uri>http://dbpedia.org/resource/Alfonso_Arau</uri>
      </binding>
    </result>
  </results>
</sparql>
  

Comments

  • 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