2

based on The Neo4j docs, executing:

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

in the neo4j browser returns the graph structure plus the rows. I wonder how can I specify ("resultDataContents":["graph","row"]) in a jQuery ajax request. I have tried this which doesn't work:

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"]} })
});

Essentially I want to build a neo4j browser clone where I can submit queries and receive the results and perhaps visualize them.

2 Answers 2

4

The result data formats are only available through the cypher http transactional endpoint : http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-return-results-in-graph-format

Which is the one used by the neo4j browser. Notice the difference between the two urls you mention.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. How can I send an XHR to those endpoints ? How does Neo4j Browser do it ? Can you please explain a bit more ?
RTFM mate, this is just the data sent that is slightly different from the legacy Cypher endpoint. The json data looks like this (as mentioned in the documentation) { "statements" : [ { "statement" : "CREATE ( bike:Bike { weight: 10 } ) CREATE ( frontWheel:Wheel { spokes: 3 } ) CREATE ( backWheel:Wheel { spokes: 32 } ) CREATE p1 = (bike)-[:HAS { position: 1 } ]->(frontWheel) CREATE p2 = (bike)-[:HAS { position: 2 } ]->(backWheel) RETURN bike, p1, p2", "resultDataContents" : [ "row", "graph" ] } ] }
Please post the actual endpoint urls you are comparing instead of links to docs. The link you provided is out of date.
Sure, then what, if after 2 years the endpoint changed also ?
2

Here is the whole procedure from query to obtaining the nodes and links of the graph.

Note that the neo4j docs (Converting Neo4j Query Results to D3 JSON) has an error : replace start with source and end with target if you want to use the graph for force directed layout.

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

    }

});

2 Comments

What is "nodes" supposed to reference here: if (idIndex(nodes,n.id) == null){? I'm assuming just a global array.
@PeteyB yeah it's missing a declaration var nodes=[], links=[];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.