2016-12-05 25 views
0

我試圖插入下面的測試文檔:Marklogic Node.js API:如何獲取嵌入三元組所在的文檔?

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

然後我問如下:

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

結果表明我三聯的價值,但如果我也想獲得什麼三重嵌入的整個文檔?是否也可以通過文檔中的其他字段進行過濾?

回答

0

我建議分兩個階段做:

  1. 運行SPARQL將返回文檔的URI。
  2. 運行文檔搜索以返回這些文檔,可選地進一步受到額外條件的限制。

爲此,您需要在文檔中嵌入三元組,列出文檔本身的文檔uri。

HTH!

3

沒有辦法檢索包含SPARQL查詢結果的文檔,因爲這些結果可能不是特定文檔中存在的三元組(相反,它會返回由「1」組成的「解決方案」或更多值)。

如果你知道你正在尋找一個特定的三元組,並且你希望擁有這個三元組的文檔,我通常會使用cts:triple-range-query;但是,我沒有看到通過Node.js API(或通過REST)來實現這一點的方法。考慮到這一點,我看到兩個選擇:

  1. 插入三倍,其中包括文檔的URI作爲主語或賓語,然後讓該文檔的請求(如@grtjn建議)
  2. 做出REST API擴展(使用JavaScript或XQuery),將cts:search與cts:triple-range-query一起作爲查詢的一部分;從節點呼叫該分機
相關問題