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()

Beyond use of sem:sparql to issue SPARQL queries, MarkLogic provides the cts:triples function. This gives you direct access in XQuery to the underlying index used to evaluate some SPARQL queries. Go check out its 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:

  1. Do the same query by accessing the triples index using cts:triples and return triples.
  2. Do the same query (Find people born in Brooklyn), but return their names.
  3. 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 as well using cts:triples now.

Using cts:triple-range-query()

MarkLogic also provides integration of semantic queries into its core search APIs via the cts:triple-range-query function.

In the last query above, you found names of cities mentioned in items that contain "dark magic". You (hopefully!) did this by feeding a cts:query into cts:triples(). You can also do this by feeding a cts:triple-range-query() into cts:search(). Try that.

SPARQL vs. XQuery: Some Advice

All the methods of combining queries use the same underlying index access. When is it better to use SPARQL over cts:triples()? When is it better to use "XQuery 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 parameter.

Consider using cts:triples() when you want to

  • Write an XQuery program and use XQuery style throughout.
  • Find pattern matches in the triple index (no sub-queries, no ASK, and so on).

Consider using cts:triple-range-query() when you want to

  • Write an XQuery program and use the XQuery style throughout.
  • Find pattern matches in the triple index (no sub-queries, no ASK, and so on).
  • Compose this query with other cts:query's in a cts:search() (like, for example as cts:word-query or cts:and-query.)

Want to do more?

Try these before moving on:

  • cts:triples() can take in subject, object, predicate as a sequence. Try using cts:triples with more than one subject.
  • cts:triples() 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 Together

Next Steps

Comments