2015-04-03 129 views
2

一個Neo4j的暗號查詢的圖形結構,執行:返回使用基於<a href="http://neo4j.com/developer/guide-data-visualization/#_presentation_svg_based_graph_interaction" rel="nofollow">Neo4j docs</a> jquery的

:POST /db/data/transaction/commit 
    {"statements":[{"statement":"MATCH path = (n)-[r]->(m) RETURN path", 
        "resultDataContents":["graph","row"]}]} 
在Neo4j的瀏覽器

返回圖結構加的行。我想知道如何在jQuery ajax請求中指定("resultDataContents":["graph","row"])。我已經試過這不工作:

var request = $.ajax({ 
    type: "POST", 
    url: "http://localhost:7474/db/data/cypher", 
    accepts: { json: "application/json" }, 
    dataType: "json", 
    contentType:"application/json", 
    data: JSON.stringify({ "query" : "MATCH (n)--(m) RETURN n,m LIMIT 2", "params": {"resultDataContents":["graph","row"]} }) 
}); 

基本上我想建立一個Neo4j的瀏覽器克隆在那裏我可以提交查詢和接收結果,也許它們可視化。

回答

2

這是從查詢到獲得圖的節點和鏈接的整個過程。

注意,Neo4j的docs(轉換Neo4j的查詢結果D3 JSON)有一個錯誤:與sourceendtarget取代start,如果你想使用圖形爲力導向佈局。

// The query 
var query= {"statements":[{"statement":"MATCH p=(n)-->(m)<--(k),(n)--(k) RETURN p Limit 100", 
    "resultDataContents":["graph","row"]}]}; 

//the helper function provided by neo4j documents 
function idIndex(a,id) { 
    for (var i=0;i<a.length;i++) { 
     if (a[i].id == id) return i;} 
    return null; 
} 
// jQuery ajax call 
var request = $.ajax({ 
    type: "POST", 
    url: "http://localhost:7474/db/data/transaction/commit", 
    accepts: { json: "application/json" }, 
    dataType: "json", 
    contentType:"application/json", 
    data: JSON.stringify(query), 
    //now pass a callback to success to do something with the data 
    success: function (data) { 
     // parsing the output of neo4j rest api 
     data.results[0].data.forEach(function (row) { 
      row.graph.nodes.forEach(function (n) { 
       if (idIndex(nodes,n.id) == null){ 
        nodes.push({id:n.id,label:n.labels[0],title:n.properties.name}); 
       } 
      }); 
      links = links.concat(row.graph.relationships.map(function(r) { 
       // the neo4j documents has an error : replace start with source and end with target 
       return {source:idIndex(nodes,r.startNode),target:idIndex(nodes,r.endNode),type:r.type}; 
      })); 
     }); 
     var graph = {nodes:nodes, links:links}; 

     // Now do something awesome with the graph! 

    } 

}); 
+0

什麼是「節點」應該在這裏引用:if(idIndex(nodes,n.id)== null){'?我假設只是一個全球陣列。 – 2016-10-26 13:44:50

+0

@PeteyB是的,它缺少一個聲明'var nodes = [],links = [];' – 2017-02-01 16:33:24

4

結果數據格式只能通過暗號HTTP事務端點:http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-return-results-in-graph-format

這是由Neo4j的瀏覽器使用的一個。注意你提到的兩個網址之間的區別。

+0

謝謝。我怎樣才能將XHR發送到這些端點? Neo4j Browser如何做到這一點?你能再解釋一下嗎? – MostafaMV 2015-04-03 23:00:48

+2

RTFM mate,這只是發送的數據,與傳統的Cypher端點略有不同。 json數據如下所示(如文檔中所述):{CREATE(bike:Bike {weight:10})CREATE(frontWheel:Wheel {spoke:3})CREATE (backWheel:Wheel {spoke:32})CREATE p1 =(bike) - [:HAS {position:1}] - >(frontWheel)CREATE p2 =(bike) - [:HAS {position:2}] - >( backWheel)RETURN bike,p1,p2「, 」resultDataContents「:[」row「,」graph「] }] – 2015-04-03 23:10:56

+0

請發佈您正在比較的實際端點url,而不是鏈接到文檔。您提供的鏈接已過時。 – 2017-02-01 16:18:21

相關問題