问题
I have a dense node problem and to get around this I've been using indexing to perform some matching using the native Java API, but I've been giving Cypher a second look and wanted to know if this could be done. Currently my pattern looks like this in Java Code:
Node startNode = db.getNodeById(1);
Index<Relationship> relationshipIndex = db.index.forRelationships("relationships");
for(Relationship relationship1 : startNode.getRelationships(Direction.OUT)) {
Node otherNode = relationship.getOtherNode(startNode);
String indexValue = (String)otherNode.getProperty("indexValue");
for(Relationship relationship2 : relationshipIndex.get("indexKey", indexValue)){
Node endNode = relationship.getOtherNode(startNode);
//Processing on the endNode
}
}
How would I translate this to cypher? Something like:
START startNode=node(1)
MATCH startNode-[r1]->otherNode
WITH node:relationships("indexValue:" + otherNode.indexValue) as r2
RETURN r2.endNode //Notation for getting node off relationship?
I don't really see anywhere to just get a relationship, then get the end node through a relationship like this.
回答1:
Yeah, in 2.0 there's a startNode(rel)/endNode(rel) function, but it's not there for 1.9 or earlier.
http://console.neo4j.org/r/4807s7
So in theory you could do this:
start rel=rel:rel_auto_index(indexKey="value")
with endNode(rel) as n
match n-->m
return *
And that would avoid ever touching the startNode of the relationship (unless there's a pointer back to it from n
).
来源:https://stackoverflow.com/questions/17305200/performing-match-by-index-lookup-with-cypher