Cypher: how to map relationships - node - relation inside the path

I'm new to Cypher and Neo4j (and stack overflows for that matter), so hopefully this question has a simple answer. I read the documentation and Google for hours, but could not find anything to answer this question.

Apparently I need more reputation for posting images, so I will do my best with the text.

I can find the path from one node to another, for example:

    MATCH path = (a {name:"a"})-[*]-(x {name:"x"})
    RETURN extract(r IN nodes(path) | r);

Which could return something like the following two paths:

    (a)-[:RED]->(b)<-[:BLUE]-(c)-[:RED]->(f)<-[:RED]-(g)-[:BLUE]->(h)<-[:RED]-(x)

    (z)-[:RED]->(h)<-[:RED]-(x)

So far so good. Of course, there are many other connections and nodes in the database that connect to these nodes, but this is the right way.

I need to find one node along a path that includes two RED relationships:

    -[:RED]->(findme)<-[:RED]-

In the two examples above, the paths findme = (f) and (h).

. , ( ). , , RED-.

, :

    MATCH (a)-[*]-(b)-[:RED]->(findme)<-[:RED]-(c)-[*]-(x) RETURN findme;

, 5 , 3 .

?

+4
3

! , * /. , , . * " ".

MATCH (a)-[*]-(b)-[:RED]->(findme)<-[:RED]-(c)-[*]-(x)
WHERE a.name = "a" AND x.name = "x"
RETURN findme

(a)-[:RED]->(findme)<-[:RED]-(x)

. , . (a)-[*]-(b) (c)-[*]-(x) " ", , .

* " ", . , , .

MATCH (a)-[*0..]-(b)-[:RED]->(findme)<-[:RED]-(c)-[*0..]-(x)
WHERE a.name = "a" AND x.name = "x"
RETURN findme

, " ", , , , .

, " " , node. node . , three- node,

node a node b,

-

a b node

, 5 node (a {name: "a"}) (x {name: "x"}) node (a {name: "a2"}) (x {name: "x2"}). ( name WHERE). , . node , - - "node a node b, -depth" just means "a b node".

+1

:

MATCH p=(a {name:"a"})-[*]-(x {name:"x"})
WITH nodes(p) AS nodes
UNWIND nodes AS n
WITH n WHERE exists(()-[:RED]->(n)<-[:RED]-())
RETURN n
  • MATCH a x.
  • node , RED
+1

, , :)

:

MATCH path=(a {name:"a"})-[*]-(x {name:"x"})
RETURN filter(x in nodes(p) WHERE size((x)<-[:RED]-()) = 2) as n

+1

+1
source

Source: https://habr.com/ru/post/1613924/


All Articles