简体   繁体   中英

Neo4j cypher - unidirectional and bidirectional in single relationship query?

I have a situation where I need to match a variable length path for either one type of relationship A or another type of relationship B . However, the relationship type A is bidirectional, whereas the relationship type B is unidirectional.

eg If both relationship types were bidirectional, I could use the following match statement:

MATCH (:Something {property: "value"}) -[:A|B*]- (n:Something)

But since the relationship type B is unidirectional, I would need something like this:

MATCH (:Something {property: "value"}) (-[:A]- OR <-[:B]-)* (n:Something)

One solution I could use is to create the reverse relationship type for all A as INVERSE_A and then use:

MATCH (:Something {property: "value"}) <-[:A|INVERSE_A|B*]- (n:Something)

But then I would need to create INVERSE_A for every A relationship, and that would make things messy for this one query.

Is there a cleaner way to combine both a unidirectional and bidirectional relationship into a single match statement?

Cypher is currently limited in this way.

If you have access to APOC Procedures , you can use the path expander procs to provide the relationships to expand, with the ability to provide the direction (or lack of) for each type independently. This particular procedure yields paths, so you'll want to get the last node of the path.:

MATCH (start:Something {property: "value"})
CALL apoc.path.expand(start, 'A|<B', 'Something', 1, -1) YIELD path
// parameters are: (startNode, relationshipFilter, labelFilter, minLevel, maxLevel)
WITH last(nodes(path)) as n
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM