简体   繁体   中英

Neo4J/Cypher: is it possibile to filter the length of a path in the where clause?

Let's suppose we have this simple pattern:

p=(a)-[r]-(b)

where nodes a and b have their own properties already set in the WHERE clause (eg a:Movie AND a.title = "The Matrix" AND b:Movie). I'd like to add another condition in the WHERE clause like

LEGHT(p) =2 OR LENGTH(p)>6

(not the correct syntax, I know)

As far as I know, it is possibile to specify the length of a path in the MATCH clause with the syntax [r*min..max] but that doesn't cover the case I'm looking for.

Any help would be appreciated =)

Yes, that does work in neo4j, exactly as you've specified.

Sample data:

create (a:Foo)-[:stuff]->(b:Foo)-[:stuff]->(c:Foo);

Then this query:

MATCH p=(x:Foo)-[:stuff*]->(y:Foo) 
WHERE length(p)>1 RETURN p;

Returns only the 2-step path from a->b->c and not either one-step path ( a->b or b->c )

Does this work for you?

MATCH p=(a)-[r*2..]-(b)
WHERE LENGTH(r) = 2 OR LENGTH(r) > 6
RETURN p

Note that with a large DB this query can take a very long time, or not finish, since the MATCH clause does not set an upper bound on the path length.

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