admin管理员组文章数量:1026989
I'm having a large Neo4j database, below is the small subset of nodes and relationships.
I have 3 different kind of nodes:
- (Pink) Customers
- (Blue) Transactions
- (Yellow) Terminals
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.
I'm having a large Neo4j database, below is the small subset of nodes and relationships.
I have 3 different kind of nodes:
- (Pink) Customers
- (Blue) Transactions
- (Yellow) Terminals
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.
Share Improve this question edited Nov 19, 2024 at 10:27 Holyamirali asked Nov 18, 2024 at 18:59 HolyamiraliHolyamirali 366 bronze badges1 Answer
Reset to default 0To 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.
I'm having a large Neo4j database, below is the small subset of nodes and relationships.
I have 3 different kind of nodes:
- (Pink) Customers
- (Blue) Transactions
- (Yellow) Terminals
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.
I'm having a large Neo4j database, below is the small subset of nodes and relationships.
I have 3 different kind of nodes:
- (Pink) Customers
- (Blue) Transactions
- (Yellow) Terminals
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.
Share Improve this question edited Nov 19, 2024 at 10:27 Holyamirali asked Nov 18, 2024 at 18:59 HolyamiraliHolyamirali 366 bronze badges1 Answer
Reset to default 0To 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.
本文标签: databaseHow to find coconnected nodes with 39k39 relationshipsNeo4jStack Overflow
版权声明:本文标题:database - How to find co-connected nodes with 'k' relationships? - Neo4j - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600148a2158407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论