roytang.net Posts Photos Archives About

I tried to insert the following test document:

db.documents.write(
    {
        uri: "/test/doc1.json",
        contentType: "application/json",
        collections: "test",
        content: {
            name : "Peter",
            hobby: "Sleeping",
            other: "Some other info",
            "triple": {
                "subject": {   
                    "datatype": "http://example.com/name/",  
                    "value": "Peter"   
                },   
                "predicate": {     
                    "datatype": "http://example.com/relation/",  
                    "value": "livesin"   
                },   
                "object": {     
                    "datatype": "http://example.com/location/",  
                    "value": "Paris"   
                }
            }
        }
    }
  ).
  result(function(response){
    console.log("Done loading");
  });

Then I queried as follows:

var query = [
  'SELECT ?s ?p ?o' ,
  'WHERE { ?s ?p ?o }' ,
];
db.graphs.sparql('application/sparql-results+json', query.join('\n')
).result(function (result) {
  console.log(JSON.stringify(result, null, 2));
}, function(error) {
  console.log(JSON.stringify(error, null, 2));
});

The results showed me the values of the triple, but what if I also want to get the entire document where the triple was embedded? Is it also possible to filter by other fields in the document?

# / / notes / #nodejs #sparql #marklogic #questions #stackoverflow #software-development / Syndicated: stackexchange / 💬 2

Last modified at: Jan. 17, 2021, 5:13 a.m. Source file

Comments

grtjn said...

I'd recommend doing it in two stages:

  1. Run a sparql that will return document uris.
  2. Run a document search to return those documents, optionally further constrained with extra criteria.

For this you will need to embed triples in your documents listing the document uri of the documents themselves.

HTH!

Dave Cassel said...

There isn't a way to retrieve the document that contains the result of a SPARQL query, because those results may not be a triple that exists within a particular document (instead, it returns a "solution" consisting of 1 or more values).

If you know you are looking for a particular triple, and you want the document that holds that triple, I would normally say to use a cts:triple-range-query; however, I don't see a way to do that through the Node.js API (or through REST, for that matter). With that in mind, I see two choices:

  1. insert a triple that includes the document's URI as the subject or object, then make a request for that document (as @grtjn suggested)
  2. make a REST API extension (using either JavaScript or XQuery) that calls cts:search with cts:triple-range-query as part of the query; call that extension from Node