Using the Triple Index
In this brief exercise, we make use of the triple index outside of SPARQL. We rely on the data you've previously loaded and we build on some of the queries we did in the previous exercise.
Using cts:triples() (XQuery) or cts.triples() (JavaScript)
Beyond use of sem:sparql
(sem.sparql in JavaScript
) to issue SPARQL queries, MarkLogic provides the
cts:triples
function (cts.triples in JavaScript
).
This gives you direct access in XQuery or JavaScript to the underlying index used to evaluate some SPARQL queries.
Go check out its XQuery function signature or JavaScript function signature in the docs
and come back to work out the questions below.
In an earlier exercise we did a simple SPARQL SELECT to find people born in Brooklyn:
- Do the same query by accessing the triples index using
cts:triples
orcts.triples
and return triples. - Do the same query (Find people born in Brooklyn), but return their names.
-
You can also go back and look at the query we did to find cities mentioned in items that contain "dark magic".
You can do that entirely in XQuery or JavaScript as well using
cts:triples
orcts.triples
now.
Using cts:triple-range-query() or cts.tripleRangeQuery()
MarkLogic also provides integration of semantic queries into its core search APIs
via the XQuery cts:triple-range-query
or JavaScript cts.tripleRangeQuery
function.
In the last query above, you found names of cities mentioned in items that
contain "dark magic". You (hopefully!) did this by feeding, in XQuery, a
cts:query
into cts:triples()
or feeding, in JavaScript, a
cts.query
into cts.triples()
. You can also do this in XQuery
by feeding a cts:triple-range-query()
into
cts:search()
or, in JavaScript, by feeding a cts.tripleRangeQuery()
into
cts.search()
. Try that.
SPARQL vs. XQuery or JavaScript: Some Advice
All the methods of combining queries use the same underlying index access. When is it better to use SPARQL over cts:triples in XQuery or cts.triples in JavaScript? When is it better to use "XQuery or JavaScript on top" over "SPARQL on top" for combinations?
Consider using SPARQL when you
- Have existing SPARQL queries that you want to re-use
- Have SPARQL expertise and feel more comfortable expressing (part of) your query that way
- Want to use SPARQL features that are not available in cts:triples(), such as
- PREFIX (for convenience and brevity)
- sub-queries (for richer queries)
- SPARQL queries other than SPARQL SELECT (i.e. CONSTRUCT, ASK, or DESCRIBE) Note: you can restrict your SPARQL query to a set of documents by passing in a cts:query (XQuery) or cts.query (JavaScript) parameter.
Consider using cts:triples in XQuery or cts.triples in JavaScript when you want to
- Write an XQuery or JavaScript program and use XQuery or JavaScript style throughout.
- Find pattern matches in the triple index (no sub-queries, no ASK, and so on).
Consider using cts:triple-range-query() in XQuery or cts.tripleRangeQuery() in JavaScript when you want to
- Write an XQuery or JavaScript program and use the XQuery or JavaScript style throughout.
- Find pattern matches in the triple index (no sub-queries, no ASK, and so on).
- Compose this query with other XQuery cts:query's in a cts:search() or JavaScript cts.query's in a cts.search. For example, like a cts:word-query or cts:and-query in XQuery or cts.wordQuery or cts.andQuery in JavaScript.
Want to do more?
Try these before moving on:
-
cts:triples()
in XQuery (cts.triples()
in JavaScript) can take in subject, object, predicate as a sequence. Try usingcts:triples
orcts.triples
with more than one subject. -
cts:triples()
(XQuery) orcts.triples()
(JavaScript) can take an operator argument, which operates over the object. If the object is the integer 21 and the operator is "<", only triples with an object <21 will be returned.
References
Query Console Workspace ts-triple-index.xml.SPARQL and XQuery/JavaScript Together
Next Steps