Make new edge between two nodes if at least three broker nodes exist

Hello,
I have a graph and I'm working with connections between nodes A and B with a broker node X as follows:
(A)-[]-(X)-[]-(B)

Now I need to add a new edge between A and B in a case that there are at least 3 distinct broker nodes X, e.g.:

(A)-[]-(X1)-[]-(B)
(A)-[]-(X2)-[]-(B)
(A)-[]-(X3)-[]-(B)

I'm failing at the match part while I do not know how to properly count the broker nodes X and how to add it into the where part of the query, my try:

match p=(A)-[]-(X)-[]-(B) 
where
count(X) > 3
return p

but this does not work and I understand that it won't work like this. Can you help me please?

Thank you very much in advance.

Hi @ldpubsec

CREATE (a:A)-[:CONNECTION]->(:X)-[:CONNECTION]->(b:B),
       (a)-[:CONNECTION]->(:X1)-[:CONNECTION]->(b),
       (a)-[:CONNECTION]->(:X2)-[:CONNECTION]->(b),
       (a)-[:CONNECTION]->(:X3)-[:CONNECTION]->(b)

The code is not cool but work.

MATCH (a:A)-[]-(X)-[]-(b:B)
WITH a,b,count(X) AS countx
MATCH p=(a)-[]-()-[]-(b)
  WHERE countx > 3
RETURN p

1 Like

Thank you! This works!

May I ask how does it work? I'm a little puzzled by the double match, the first is ok with the following with, but why is there the second match? Thank you in advance.

@ldpubsec
If you don't need the return value of p, how about this?

MATCH (a:A)-[]-(X)-[]-(b:B)
WITH a,b,count(X) AS countx
  WHERE countx > 3
RETURN a,b
2 Likes