roytang.net Posts Photos Archives About

I inserted a test document using the Node.js API:

  db.documents.write(
    {
        uri: "/test/doc1.json",
        contentType: "application/json",
        collections: "test",
        content: {
            name : "Peter",
            "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 tried querying it via SPARQL on the Marklogic console webapp:

SELECT ?s ?p ?o
  WHERE { ?s ?p ?o }

The query above works fine, it retrieves the test triple I inserted above

However, if I want to filter by the literal value "Paris", I tried the following:

PREFIX loc: <http://example.com/location/>

SELECT ?s ?p 
  WHERE { ?s ?p loc:Paris }

But in this case I got zero results. Is this syntax incorrect? I was just following how the queries looked in https://docs.marklogic.com/guide/semantics/semantic-searches

What is the proper way to do it?

Comments

grtjn said...

You referred to this blog article: http://developer.marklogic.com/blog/managed-vs-unmanaged-triples

Unfortunately it is not showing the best example. A better way of writing a triple would be something like:

{
    "triples": [{
        "triple": {
            "subject": "http://dbpedia.org/resource/Argo_(2012_film)",
            "predicate": "http://dbpedia.org/ontology/producer",
            "object": "http://dbpedia.org/resource/George_Clooney"
        }
    }]
}

By using a value property inside subject, predicate and/or object, you tell MarkLogic to read it as a value, not as an iri. So, with your example you should either use:

SELECT ?s ?p 
WHERE {
  ?s ?p ?o.
  FILTER( STR(?o) = "Paris" ) 
}

Or change your JSON to:

       "triple": {
            "subject": "http://example.com/name/Peter",   
            "predicate":http://example.com/relation/livesin",   
            "object": http://example.com/location/Paris"
        }

HTH!