This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
store-triples-as-properties.xqy
60 lines (50 loc) · 1.8 KB
/
store-triples-as-properties.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(: This script copies all the facet-related triples
associated with each document to that document's
properties fragment.
This effectively enables us to facet on RDF data.
NOTE: it will overwrite the document's properties,
so the assumption is that no other properties
are being used.
:)
xquery version "1.0-ml";
import module namespace sem="http://marklogic.com/semantics"
at "/MarkLogic/semantics.xqy";
declare option xdmp:mapping "false";
(: Get all the facet info from our application config :)
declare variable $facet-configs :=
xdmp:document-get(xdmp:modules-root()||"application/config/facets.xml")
/facets/facet;
(: For the given document id, run the SPARQL query
associated with each configured facet, binding
the $facetProperty and $docId variables (effectively,
via regex-replacement, which performs much better).
:)
declare function local:triples($doc-id) {
$facet-configs !
(
let $sparql-src :=
if (sparql)
then sparql
else let $id := @sparql-idref
return //sparql[@id eq $id]
let $sparql := ../sparql-prefixes||$sparql-src
let $sparql := replace($sparql,"\$facetProperty","<"||@rdf-property||">")
let $sparql := replace($sparql,"\$docId", "<"||$doc-id||">")
return
sem:sparql($sparql)
)
};
(: For each document in the collection... :)
collection("http://www.bbc.co.uk/news/content") !
(
let $uri := document-uri(.),
$doc-id := /*:html/*:head/@resource/string(),
$triples := local:triples($doc-id),
$xml := sem:rdf-serialize($triples,"triplexml")
return
(: Load the triples into the properties fragment :)
( xdmp:log("Setting properties for ("||position()||" of "||last()||") "||$uri),
(:xdmp:log(xdmp:quote($xml)),:)
xdmp:document-set-properties($uri, $xml)
)
)