return 使用 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 浏览器中 returns 图形结构加上行。我想知道如何在 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 浏览器克隆,我可以在其中提交查询并接收结果,也许还可以将它们可视化。

结果数据格式只能通过 cypher http 事务端点获得:http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-return-results-in-graph-format

这是neo4j浏览器使用的那个。请注意您提到的两个网址之间的区别。

这里是从查询到获取图的节点和链接的整个过程。

注意neo4j docs (Converting Neo4j Query Results to D3 JSON)有一个错误:replace start with source and end with target 如果您想使用图形进行力导向布局。

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

    }

});