<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://neowiki.ijs.si/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.ijs.si/w/index.php?action=history&amp;feed=atom&amp;title=SPARQL_queries"/>
	<link rel="alternate" type="text/html" href="https://neowiki.ijs.si/w/index.php?title=SPARQL_queries&amp;action=history"/>
	<updated>2026-11-25T12:33:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://neowiki.ijs.si/w/index.php?title=SPARQL_queries&amp;diff=115&amp;oldid=prev</id>
		<title>NeoWiki: Importing NeoWiki demo data</title>
		<link rel="alternate" type="text/html" href="https://neowiki.ijs.si/w/index.php?title=SPARQL_queries&amp;diff=115&amp;oldid=prev"/>
		<updated>2026-11-24T15:53:26Z</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 a [https://neowiki.ai/docs/operations/installation#optional-sparql-graph-stores SPARQL 1.1 graph store] such as [https://qlever.dev/ QLever] in sync with the wiki&amp;#039;s structured data on every edit, so it can be queried live. This page shows the three ways to query it, using the native vocabulary. {{#ifexist:EDM queries|The store also holds the EDM projection: see [[EDM queries]] to query it, or to join across both.}}&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 and returns the raw [https://www.w3.org/TR/sparql11-results-json/ SPARQL JSON results]. IRIs are minted under the wiki&amp;#039;s own URL, which the examples write 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;
&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 page&amp;#039;s triples live in their own named graph, &amp;lt;code&amp;gt;{{SERVER}}/graph/native/page/&amp;amp;lt;pageid&amp;amp;gt;&amp;lt;/code&amp;gt;, so a result can show which page it came 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;nw.sparqlQuery&amp;lt;/code&amp;gt; returns the same results as a Lua table, so a Scribunto module can format them. 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;
	if not nw.sparqlQuery then&lt;br /&gt;
		return &amp;quot;&amp;#039;&amp;#039;This list needs a SPARQL store.&amp;#039;&amp;#039;&amp;quot;&lt;br /&gt;
	end&lt;br /&gt;
&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 REST API, which returns the results 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;
== Learn more ==&lt;br /&gt;
&lt;br /&gt;
* [https://neowiki.ai/docs/api/query-api Query API reference]&lt;br /&gt;
* [https://neowiki.ai/docs/authoring/parser-functions Parser function reference]&lt;br /&gt;
* [https://neowiki.ai/docs/authoring/lua-api Lua API reference]&lt;br /&gt;
* [[Developers]], with the same query in Cypher for a wiki with a Neo4j backend&lt;/div&gt;</summary>
		<author><name>NeoWiki</name></author>
	</entry>
</feed>