4

In marklogic, triples can be embedded into an existing document. How can i use cts search query to return the document. An example of a document could be

<ContentVersion>
  <Name>Testing</Name>
  <Status>Approved</Status>
  <sem:triples xmlns:sem="http://marklogic.com/semantics">
   <sem:triple>
      <sem:subject>http://mycontent/content/Testing</sem:subject>
      <sem:predicate>is</sem:predicate>
      <sem:object>Approved</sem:object>
 </sem:triple>
</sem:triples>
</ContentVersion>

if try the below query

let $query := cts:word-query('Testing',"case-insensitive")
let $sparql := "PREFIX cts: <http://marklogic.com/cts#>
                DESCRIBE ?s 
                WHERE{ 
                   ?s ?p ?o .
                   FILTER cts:contains(?o, cts:word-query('Testing')) 
                }"
let $results := sem:sparql($sparql,(),("default-graph=magician"),($query))  
return(sem:rdf-serialize($results,'rdfxml'))

I get an empty result. Any ideas on why nothing is returned? I am using MarkLogic 7

C Kingsley
  • 228
  • 1
  • 10

1 Answers1

7

The cts:contains is focused to ?o, which only contains 'Approved'. That is why the sem:sparql is not returning results, not because you are using the cts query the wrong way.

(update..)

To confirm the approach is valid, I tried this and it works for me:

xquery version "1.0-ml";

let $xml := <ContentVersion>
  <Name>Testing</Name>
  <Status>Approved</Status>
  <sem:triples xmlns:sem="http://marklogic.com/semantics">
   <sem:triple>
      <sem:subject>http://mycontent/content/Testing</sem:subject>
      <sem:predicate>is</sem:predicate>
      <sem:object>Approved</sem:object>
 </sem:triple>
</sem:triples>
</ContentVersion>
return xdmp:document-insert("/test.xml", $xml, (), "magician")
;

let $query := cts:word-query('Testing',"case-insensitive")
let $sparql := "PREFIX cts: <http://marklogic.com/cts#>
                DESCRIBE ?s 
                WHERE{ 
                   ?s ?p ?o .
                   FILTER cts:contains(?o, cts:word-query('Approved')) 
                }"
let $results := sem:sparql($sparql,(),("default-graph=magician"),($query))  
return $results

Run this with QC against any database that has triple index enabled.

Are you sure you insert your docs with collection 'magician'? That is how you can get embedded triples inside a specific graph with MarkLogic.

HTH!

grtjn
  • 20,254
  • 1
  • 24
  • 35
  • I have changed the the word-query to be search for the word to be Approved and i am getting an empty query result – C Kingsley Feb 04 '16 at 16:45
  • thank you very much for your help. I would like to understand what your comment regarding inserting with the collection magician. what is the impact of this and any other recommendation on the best approach to take? – C Kingsley Feb 04 '16 at 21:07
  • 3
    With embedded triples, assigning the entire document in which you embed them to a collection with the same name as the graph to which you want to add the triples, that is how you should do it. – grtjn Feb 05 '16 at 09:45
  • OMG! this is just awesome. `cts` query inside `SPARQL`. It will be awesome if you please provide any document available to learn more about how to use `cts` inside `SPARQL` – Dixit Singla Jun 18 '18 at 14:12
  • There isn't really anything to read. It is just that you can use pretty much all MarkLogic built-in functions, provided you know the prefix namespace.. – grtjn Jun 20 '18 at 04:46