I'm having a large Neo4j database, below is the small subset of nodes and relationships.
I have 3 different kind of nodes:
It's easy to find the pattern below using Cypher, by just indicating the pattern using MATCH
.
c1 -[:MADE]-> transaction1 -[:AT]-> terminal <-[:AT]- transaction2 <-[:MADE]- c2
We consider that this means that c2
node, is a 2nd connection degree neighbour to c1
.
But this could be done simply for connection degree of 2. How can I find k
th connection degree neighbours to a given customer node, for k>2 ? I'm looking for only Customer
neigbour nodes.
I'm using Python to execute my cypher queries in neo4j. I know I can maybe append the pattern k
times in my query string to achieve this goal, but I want to know if is it possible to just indicate this in one specific cypher query.
If there's any solutions that would let me choose k
explicitly in my cypher query, that would be great. I couldn't find anything like that.
Update: I would appreciate a query which can show me the path with the relationships as well, as I would like to check manually and make sure the path is a correct one.
To get the paths to Customer
nodes that are, for example, four degrees away from a Customer
node with id = 'c1'
, you can use the following:
MATCH p =
(c1:Customer {id: 'c1'})
(()-[:MADE]->(:Transaction)-[:AT]->(:Terminal)
<-[:AT]-(:Transaction)<-[:MADE]-(:Customer)){4} (c2)
WHERE c1 <> c2
RETURN p
Note here what you refer to as 2nd degree connection would here be 1st.
That solution ignores the fact that there may be multiple paths of varying length that connect a given pair of nodes. To only return the minimum degree for each Customer
node, find the shortest path between each pair:
MATCH p = SHORTEST 1
(c1:Customer {id: 'c1'})
(()-[:MADE]->(:Transaction)-[:AT]->(:Terminal)
<-[:AT]-(:Transaction)<-[:MADE]-(:Customer)){4} (c2)
WHERE c1 <> c2
RETURN p
These solutions use quantified path patterns and shortest paths.