diff --git a/frontend/src/sparql/compile-query.ts b/frontend/src/sparql/compile-query.ts
index 3b4edafa..0a4fb5e0 100644
--- a/frontend/src/sparql/compile-query.ts
+++ b/frontend/src/sparql/compile-query.ts
@@ -1,5 +1,5 @@
import Model from '../core/model';
-import { nsTable, nsMap } from '../common-rdf/ns';
+import { source, item, nsTable, nsMap } from '../common-rdf/ns';
import userChannel from '../common-user/user-radio';
import itemsTemplate from './query-templates/items-for-source-template';
@@ -52,13 +52,9 @@ export function nodesByUserQuery(
}
export function randomNodesQuery(
- randomNodes: Model[], lastNode: Model, options: SPARQLQueryOptions = {}
+ itemQuery: boolean, options: SPARQLQueryOptions = { limit: 10 }
) {
- const data = {
- ...defaultOptions,
- randomNodes: randomNodes.map( model => model.get('value')),
- lastNode: lastNode.get('value'),
- ...options,
- };
+ const nsLength = (itemQuery ? source() : item()).length;
+ const data = { ...defaultOptions, itemQuery, nsLength, ...options };
return randomNodesTemplate(data);
}
diff --git a/frontend/src/sparql/query-templates/random-nodes-template.hbs b/frontend/src/sparql/query-templates/random-nodes-template.hbs
index 71161112..bca51875 100644
--- a/frontend/src/sparql/query-templates/random-nodes-template.hbs
+++ b/frontend/src/sparql/query-templates/random-nodes-template.hbs
@@ -1,7 +1,24 @@
-PREFIX dcterms:
-PREFIX oa:
-CONSTRUCT {?node ?p ?o}
-WHERE {
- ?node ?p ?o.
- FILTER ({{#each randomNodes}}?node = <{{this}}> ||{{/each}} ?node = <{{lastNode}}>)
+{{>sparqlPreamble}}
+
+construct {
+ ?subject ?prop ?val
+} where {
+ ?subject ?prop ?val
+ {
+ select distinct ?subject where {
+ {{#itemQuery}}
+ ?anno oa:hasBody ?subject.
+ ?subject a ?x.
+ {{/itemQuery}}
+ {{^itemQuery}}
+ ?subject a vocab:Source.
+ {{/itemQuery}}
+ bind (substr(str(?subject), {{nsLength}}) as ?serial)
+ bind (strlen(?serial) as ?cap)
+ bind (substr(?serial, ?cap) as ?suffix)
+ bind (substr(?serial, 1, ?cap - 1) as ?prefix)
+ }
+ order by md5(concat(?suffix, substr(str(rand()), 2), ?prefix))
+ limit {{limit}}
+ }
}