Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About

All entries tagged sparql.

You can subscribe to an RSS feed of this list.

Dec 2016

  • 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?

  • 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?