0
votes

I'musing ExecutionEngine in java to run cypher queries against a neo4j database.

I'd like to get all relationships that exist for a node.

My raw cypher would be:

MATCH (n:Phone{id:'you'}) MATCH n-[r:calling]->m WHERE n<>m RETURN n, r, m

I see plenty of example online that describe how I can get the results of noes from a query, but I'd like to return both the nodes n and m as well as relationship r.

Do I need to do anything different than if I was just returning nodes?

1
What is the actual question? You are already returning all "calling" relationships of n.Michael Hunger

1 Answers

2
votes

Here's how you execute cypher queries from Java.

Here's some code for how you'd get those relationships. I haven't tested this, but it's the right general approach.

String query = "MATCH (n:Phone{id:'you'}) MATCH n-[r:calling]->m WHERE n<>m RETURN n, r, m";

ExecutionEngine engine = new ExecutionEngine( db );

ExecutionResult result;
try ( Transaction ignored = db.beginTx() ) {
    result = engine.execute(query);
    ResourceIterator<Relationship> rels = result.columnAs("r");
    while(rels.hasNext()) { 
        Relationship r = rels.next();
        // Do something cool here.
    } 
} catch(Exception exc) { System.err.println("ERHMAGEHRD!!!"); }

Basically, use the columnAs() method to get a result column. Note that here it's "r" because your query is returning relationships into a variable that name.

OK, now for your question about the query. In java, I like to return as little as possible from queries. If you need it, it should be in the return clause. If you don't, then it shouldn't be.

If you want the relationships, then return them. Don't try to get at relationships by returning the nodes, then looking from there. That approach will work, but just going straight for the relationships makes more sense.