1
votes

I have a graph model consisting of two node types. Each USER has a dependency to one or more USERs via a DEPDATA node (which holds significant information on the relationship).

(:USER)-[:DEPENDS_ON]->(:DEPDATA)-[:FOR]->(:USER)

This chain can have infinite length, so the part

-[:DEPENDS_ON]->(:DEPDATA)-[:FOR]->(:USER)

can be repeated n times. It's also possible that a USER has no relationships, so this all is valid

(:USER)
(:USER)-[:DEPENDS_ON]->(:DEPDATA)-[:FOR]->(:USER)-[:DEPENDS_ON]->(:DEPDATA)-[:FOR]->(:USER)

What I like to retrieve is all USER nodes which depend on a specific USER node, regardless in wich depth.

I've already read about variable relationship length here http://graphaware.com/graphaware/2015/05/19/neo4j-cypher-variable-length-relationships-by-example.html but it seems to apply only for direct relations.

How can I make a whole part of the graph structure to be matched 0..n times?

2
Why are you storing the 'significant information' in a specific node, and not on the relationship directly? If you were, you could use a var-length relationship. - Mats Rydberg
AFAIK I can store only simple datatypes in relationships, not my own complex java classes. - Frank
I don't understand what you mean. Nodes and relationships support the same property values. Which version of Neo4j are you on? - Mats Rydberg
The 'significant information' is some kind of subtree, in this example information about the kind of dependency and rules for evaluating the dependency, consisting of one to n nodes each also interconnected by relations. I'm using spring-data 4.1.4.RELEASE with neo4j-ogm-core 2.0.5. - Frank
I see. I assumed you were on 'pure' Neo4j, but was clearly wrong. - Mats Rydberg

2 Answers

1
votes

You're using n as your upper bound, but you said the chain could be infinite length. In this situation, if you are not applying an upper bound, then you don't need to have a variable for it.

I think what you're looking for is something like this (using ID property as a placeholder for however you're uniquely identifying the dependent User node):

MATCH (dep:User{ID:123})<-[:DEPENDS_ON|FOR*0..]-(u:User)
RETURN DISTINCT u

This will match on zero or more chains of :DEPENDS_ON or :FOR relationships, finding all distinct dependent users.

0
votes

If you want the results for each "full" path starting at the "root" node in a separate collection, the following should work. (A "full" path is one that has the root node at one end and a "leaf" node at the other.)

MATCH p=(u:USER)-[:DEPENDS_ON|FOR*0..]->(root:USER {id: 111})
WHERE NOT ()-[:FOR]->(u)
RETURN [n IN NODES(p) WHERE n:USER] AS path_nodes;