0
votes

I am trying to query whole graph db using Ajax in Neo4j. Below is my ajax code.

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

It's returning the response as:

{    
    "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":[]
}

Why I am not able to get the relationship? Any suggestions would help.

Note - All the nodes are connected in Neo4j through relationship.

1

1 Answers

2
votes

You are getting the relationships back.

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

The second map is an empty map because your relationships do not have properties.

If you issue the same query in the Neo4j browser you can see that the result is the same for the rows format :

enter image description here

If you need some kind of additional metadata, like types, start nodes, end nodes, you need to specify the rest or graph resultDataContent for your query :

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

Up to you to pick up the result data content that fit your needs.

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