Hey everybody.
In the application I'm developing I have a typical task to get all the relationships between two given (non-adjacent) nodes, together with all the nodes involved.
If we try to visualize the structure of the... subgraph I want to get, in the typical case it will not be like a linear path. It will not be like a tree with one root and a lot of leaves. Rather it will be a messy picture that starts at one node, splits and joins along the way chaotically, and then all comes to the second node.
All the relationships I'm interested in will have an equal property - say, r.userID, so I feel that to just filter these relationships should be easy for the DB (I have an index on r.userID).
But when I throw nodes into the picture, it seems much more complicated.
I know that I can write something like:
MATCH path = (n1)-[r:TYPE* {userID: $userID}]->(n2)
and get nodes and relationships from the path. (Either in my app code or directly in a query.)
But AFAIK cypher will return me all the possible linear paths, and this is hugely ineffective for me. Since there can be a lot of relationships created by the same user, number of possible paths can quite possibly explode. User's relationships can form a loop or loops, for example.
And I'm actually not interested in linear paths at all. Just a bag of unordered relationships and another bag of unordered nodes will be totally fine for me - I can figure out how they are connected from the start node to the end one in the application code.
And on the contrary, if I could get all these relationships as a single messy path (can we call it a tree-like structure?) - that would be just wonderful.
So, getting paths can be (in my case) hugely ineffective.
Getting just the relationships is easy and (should be?) really efficient:
MATCH (n1)-[r:TYPE* {userID: $userID}]->(n2) RETURN r
or even:
MATCH ()-[r:TYPE* {userID: $userID, anotherProperty: $anotherProperty}]->() RETURN r
will work for me just as well.
But I don't know how to get nodes from r
(they will have startNodeElementId
and endNodeElementId
properties, but how do I get nodes from them?..)
Btw, I don't remember at the moment if cypher do allow to add {userID: $userID}
part into variable length relationship chain, but whatever, I can replace it with WHERE
, it's all the same.
Any advice how to get out of this conundrum?
I feel like this should be easy
Any help is appreciated.