user3853495
user3853495

Reputation: 1

Fixed length of variable path

I have some linked list structure:

(post:Post)-[:NEXT]->(next_post:Post)-[:NEXT]->....

I can query it by simple statement:

MATCH (start:Post{Id:{post}})-[:NEXT*0..19]->posts
RETURN posts

It returns 20 posts in response.

But if my query is like that:

MATCH (start:Post{Id:{post}})-[:NEXT*0..19]->post
WHERE not post:Deleted
RETURN posts

then I will certainly not get 20 posts (i.e. if I have 3 posts with :Deleted label, then I will get 17 posts)

How can I achieve fixed length of variable path with some conditions?

In total, I want to get same fixed amount of nodes in variable length path with upper bound despite of predicates. Something like that:

MATCH (start:Post{Id:{post}})-[rels:NEXT*0..]->post
WHERE ANY (rel IN rels WHERE NOT ENDNODE(rel):Deleted)
RETURN post

but with upper limit of traversal.

Upvotes: 0

Views: 96

Answers (1)

Nicole White
Nicole White

Reputation: 7800

Filter out the deleted posts beforehand:

MATCH (posts:Post)
WHERE NOT posts:Deleted
WITH posts
MATCH (start:Post{Id:{post}})-[:NEXT*0..19]->(posts)
RETURN posts

Upvotes: 1

Related Questions