<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://neowiki.dev/w/index.php?action=history&amp;feed=atom&amp;title=SPARQL_queries</id>
	<title>SPARQL queries - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://neowiki.dev/w/index.php?action=history&amp;feed=atom&amp;title=SPARQL_queries"/>
	<link rel="alternate" type="text/html" href="https://neowiki.dev/w/index.php?title=SPARQL_queries&amp;action=history"/>
	<updated>2026-07-18T12:49:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://neowiki.dev/w/index.php?title=SPARQL_queries&amp;diff=371&amp;oldid=prev</id>
		<title>NeoWiki: Importing NeoWiki demo data</title>
		<link rel="alternate" type="text/html" href="https://neowiki.dev/w/index.php?title=SPARQL_queries&amp;diff=371&amp;oldid=prev"/>
		<updated>2026-07-17T22:42:50Z</updated>

		<summary type="html">&lt;p&gt;Importing NeoWiki demo data&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;NeoWiki keeps [https://github.com/ProfessionalWiki/NeoWiki/blob/master/docs/operations/installation.md#optional-sparql-graph-stores SPARQL 1.1 graph stores] such as [https://qlever.dev/ QLever] in sync with the wiki&amp;#039;s structured data: each page becomes a named graph of RDF triples — one per [[RDF and ontology mappings|projection]] the store holds — updated on each edit and dropped on deletion. This wiki&amp;#039;s store holds the native projection. This page demonstrates the three ways to query it. It is imported only into wikis that have a SPARQL store configured, like the NeoWiki development stack.&lt;br /&gt;
&lt;br /&gt;
== Query with a parser function ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;{{#sparql_raw}}&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; parser function runs a read-only SPARQL query against the first configured store and returns the raw [https://www.w3.org/TR/sparql11-results-json/ SPARQL JSON results]. Entity, schema, and property IRIs are minted under the wiki&amp;#039;s RDF base URI, which defaults to the wiki&amp;#039;s own server URL — the examples below write it as &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;{{SERVER}}&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; so they work on any wiki.&lt;br /&gt;
&lt;br /&gt;
Source:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;mediawiki&amp;quot;&amp;gt;&lt;br /&gt;
{{#sparql_raw:SELECT ?museum ?visitors WHERE { ?m &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&amp;gt; &amp;lt;{{SERVER}}/schema/Museum&amp;gt; . ?m &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?museum . ?m &amp;lt;{{SERVER}}/prop/Annual_visitors&amp;gt; ?visitors } ORDER BY DESC(?visitors) LIMIT 5}}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&lt;br /&gt;
{{#sparql_raw:SELECT ?museum ?visitors WHERE { ?m &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&amp;gt; &amp;lt;{{SERVER}}/schema/Museum&amp;gt; . ?m &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?museum . ?m &amp;lt;{{SERVER}}/prop/Annual_visitors&amp;gt; ?visitors } ORDER BY DESC(?visitors) LIMIT 5}}&lt;br /&gt;
&lt;br /&gt;
Each graph is named for the projection and the page it holds — &amp;lt;code&amp;gt;{{SERVER}}/graph/native/page/&amp;amp;lt;pageid&amp;amp;gt;&amp;lt;/code&amp;gt; — so results can carry provenance, and one store can hold several projections without a page&amp;#039;s triples in one overwriting the other&amp;#039;s. This query lists people together with the graph each one comes from:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;mediawiki&amp;quot;&amp;gt;&lt;br /&gt;
{{#sparql_raw:SELECT ?graph ?person WHERE { GRAPH ?graph { ?p &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&amp;gt; &amp;lt;{{SERVER}}/schema/Person&amp;gt; . ?p &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?person } } ORDER BY ?person LIMIT 5}}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&lt;br /&gt;
{{#sparql_raw:SELECT ?graph ?person WHERE { GRAPH ?graph { ?p &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&amp;gt; &amp;lt;{{SERVER}}/schema/Person&amp;gt; . ?p &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?person } } ORDER BY ?person LIMIT 5}}&lt;br /&gt;
&lt;br /&gt;
== Query from Lua ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;mw.neowiki.sparqlQuery&amp;lt;/code&amp;gt; returns the same results document as a Lua table, so Scribunto modules can format it. From [[Module:LuaExample]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function p.largestMuseums( frame )&lt;br /&gt;
	local base = mw.site.server&lt;br /&gt;
	local result = nw.sparqlQuery(&lt;br /&gt;
		&amp;#039;SELECT ?label ?visitors WHERE { &amp;#039; ..&lt;br /&gt;
		&amp;#039;?m &amp;lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#type&amp;gt; &amp;lt;&amp;#039; .. base .. &amp;#039;/schema/Museum&amp;gt; . &amp;#039; ..&lt;br /&gt;
		&amp;#039;?m &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?label . &amp;#039; ..&lt;br /&gt;
		&amp;#039;?m &amp;lt;&amp;#039; .. base .. &amp;#039;/prop/Annual_visitors&amp;gt; ?visitors &amp;#039; ..&lt;br /&gt;
		&amp;#039;} ORDER BY DESC(?visitors) LIMIT 3&amp;#039;&lt;br /&gt;
	)&lt;br /&gt;
&lt;br /&gt;
	local list = {}&lt;br /&gt;
	for _, binding in ipairs( result.results.bindings ) do&lt;br /&gt;
		list[#list + 1] = &amp;#039;* &amp;#039; .. binding.label.value .. &amp;#039; (&amp;#039; .. binding.visitors.value .. &amp;#039; visitors)&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	return table.concat( list, &amp;#039;\n&amp;#039; )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Invoke it from wikitext:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;mediawiki&amp;quot;&amp;gt;&lt;br /&gt;
{{#invoke:LuaExample|largestMuseums}}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&lt;br /&gt;
{{#invoke:LuaExample|largestMuseums}}&lt;br /&gt;
&lt;br /&gt;
== Query over REST ==&lt;br /&gt;
&lt;br /&gt;
The same queries work over the wiki&amp;#039;s REST API, which returns the results document as JSON:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;POST {{SERVER}}{{SCRIPTPATH}}/rest.php/neowiki/v0/query/sparql&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;json&amp;quot;&amp;gt;&lt;br /&gt;
{ &amp;quot;query&amp;quot;: &amp;quot;SELECT ?label WHERE { ?s &amp;lt;http://www.w3.org/2000/01/rdf-schema#label&amp;gt; ?label } LIMIT 5&amp;quot; }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For details, see the [https://github.com/ProfessionalWiki/NeoWiki/blob/master/docs/reference/query-api.md query API], [https://github.com/ProfessionalWiki/NeoWiki/blob/master/docs/reference/parser-functions.md parser function], and [https://github.com/ProfessionalWiki/NeoWiki/blob/master/docs/reference/lua-api.md Lua] documentation. For querying the property graph with Cypher instead, see [[Developers]].&lt;/div&gt;</summary>
		<author><name>NeoWiki</name></author>
	</entry>
</feed>