In Cypher returning neighbours would look like this:
START n=node({nodeId}) MATCH n--m RETURN m;
I struggle to create a Java-plugin that does the same. Afterwards i want to see the properties of the returned neighbours and so on. This is my code where I try to get the Relationships and their EndNodes. What am I doing wrong? I only get returned the node itself but nothing from the "while-Loop".
import java.util.Iterator;
import java.util.stream.Stream;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.logging.Log;
import org.neo4j.procedure.*;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
public class Correlation {
@Context
public GraphDatabaseService db;
@Context
public Log log;
@UserFunction(value = "boris.getNeighbours")
@Description("boris.getNeighbours- finds Nodes one step away")
public Node passName2(@Name("nodeId") long nodeId)
{
Node node = db.getNodeById( nodeId );
Iterable<Relationship> rels = node.getRelationships();
Iterator<Relationship> I_rel = rels.iterator();
while (I_rel.hasNext()) {
Relationship rel = I_rel.next();
Node endNode = rel.getEndNode();
return endNode;
}
return node;
}
}
This works: