Marklogic Cts:search with sparql
Asked Answered
E

1

5

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

Egger answered 4/2, 2016 at 15:53 Comment(0)
M
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!

Maieutic answered 4/2, 2016 at 16:35 Comment(5)
I have changed the the word-query to be search for the word to be Approved and i am getting an empty query resultEgger
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?Egger
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.Maieutic
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 SPARQLParalogism
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..Maieutic

© 2022 - 2024 — McMap. All rights reserved.