2016-12-05 48 views
0

我插入使用Node.js的API測試文檔:如何使用SPARQL查詢Marklogic三元組?

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"); 
    }); 

然後我試圖在Marklogic控制檯web應用程序通過SPARQL查詢它:

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

上面的查詢工作正常,它檢索測試三重我上面插入

但是,如果我想用文字值「巴黎」來過濾,我試過如下:

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

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

但在這種情況下,我得到零結果。這個語法不正確?我只是下面的查詢中https://docs.marklogic.com/guide/semantics/semantic-searches

是什麼做的正確方法是什麼樣子?

回答

4

你提到這個博客文章:http://developer.marklogic.com/blog/managed-vs-unmanaged-triples

遺憾的是它沒有顯示最好的例子。寫一個三重的更好的方式是這樣的:

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

通過使用內部的主語,謂語和/或對象value屬性,你告訴MarkLogic閱讀它作爲一個值,而不是作爲一個IRI。所以,你的榜樣,你要麼使用:

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

或更改您的JSON到:

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

HTH!

+0

優秀。謝謝! –