最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to find co-connected nodes with 'k' relationships? - Neo4j - Stack Overflow

programmeradmin1浏览0评论

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 kth 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 kth 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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论