簡體   English   中英

使用jquery返回Neo4j密碼查詢的圖結構

[英]return the graph structure of a Neo4j cypher query using jquery

基於Neo4j docs ,執行:

: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瀏覽器克隆,在其中我可以提交查詢並接收結果,並可視化它們。

結果數據格式僅可通過密碼http事務終結點使用:http: //neo4j.com/docs/stable/rest-api-transactional.html#rest-api-return-results-in-graph-format

neo4j瀏覽器使用的那個。 請注意您提到的兩個URL之間的區別。

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

請注意,neo4j 文檔 (將Neo4j查詢結果轉換為D3 JSON)有一個錯誤:如果要將圖形用於強制定向布局,請替換以source start和以target end

// 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!

    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM