在Neo4j中使用Ajax使用Cypher查询整个图形DB

时间:2015-10-02 11:43:32

标签: neo4j cypher

我试图在Neo4j中使用Ajax查询整个图形数据库。下面是我的ajax代码。

$.ajax({  
    type: "POST",  
    url: "http://localhost:7474/db/data/transaction/commit ",  
    dataType: "json",  
    contentType: "application/json;charset=UTF-8",  
    data: JSON.stringify({"statements": [{"statement": "MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN n,r"}]}),  
    success: function (data, textStatus, jqXHR) {  
        $(".neo4jResponse").html(JSON.stringify(data));  
    },  
    error: function (jqXHR, textStatus, errorThrown) {  
        alert("Error");  
    }  
});`

它将响应返回为:

{    
    "results":
    [
        {
            "columns":["n","r"],
            "data":
            [
                {"row":[{"title":"Tourism","name":"Tourism"},{}]},
                {"row":[{"title":"Tourism","name":"Tourism"},{}]},
                {"row":[{"title":"Coastal Debris","name":"Coastal Debris"},{}]},
                {"row":[{"title":"Coastal Debris","name":"Coastal Debris"},{}]},
                {"row":[{"title":"Quality","name":"Quality"},{}]},
                {"row":[{"title":"Quality","name":"Quality"},{}]},
                {"row":[{"title":"Recreational Value","name":"Recreational Value"},{}]},
                {"row":[{"title":"Recreational Value","name":"Recreational Value"},{}]},
                {"row":[{"title":"Eco-Tourism Incentives","name":"Eco-Tourism Incentives"},{}]},
                {"row":[{"title":"Eco-Tourism Incentives","name":"Eco-Tourism Incentives"},{}]},
                {"row":[{"title":"Eco-Tourism","name":"Eco-Tourism"},{}]},
                {"row":[{"title":"Eco-Tourism","name":"Eco-Tourism"},{}]}
            ]
        }
    ],
    "errors":[]
}

为什么我无法得到关系? 任何建议都会有帮助。

注意 - 所有节点都通过关系在Neo4j中连接。

1 个答案:

答案 0 :(得分:2)

你正在恢复关系。

[{"title":"Tourism","name":"Tourism"},{}]

第二张地图是空地图,因为您的关系没有属性。

如果您在Neo4j浏览器中发出相同的查询,您可以看到行格式的结果相同:

enter image description here

如果您需要某种其他元数据,例如类型,起始节点,结束节点,则需要为查询指定restgraph resultDataContent:

data: JSON.stringify({"statements": [{"statement": "MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN n,r"}, "resultDataContents":["row", "graph", "rest"]})

由您自行选择适合您需求的结果数据内容。

参考:http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-return-results-in-graph-format