cancel
Showing results for 
Search instead for 
Did you mean: 

How to write labelFilter with unspecified number of the same label nodes

Hi everyone,

I am new to Neo4j APOC and am learning how to write advanced Cypher queries. I have read that APOC could expand the path by configuring the labelFilters and relationshipFilters. I have a specific use case where I tried to write a query for. Would appreciate if there is help for this. Thanks!

The use case is: I would like to find the paths satisfying the below criteria:

1. A defined starting node N1 and terminating node N2

2. Between N1 and N2, there should only contain two types of nodes: L1 and L2 in this order. For example: N1 -> L1 -> L1 -> L1 -> L2 -> L2 -> N2 

3. In-between nodes could happen multiple times. Basically, N1 -> *L1 -> *L2 -> N2

4. Between nodes of L1, I would like to filter relations for R1; between nodes of L2, I would like to filter relations for R1 or R2

If anyone could help, that would be great! Thanks so much!

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

I reviewed the apoc 'path' procedures and did not find one that allows to specify a pattern of nodes. Maybe @bennu_neo will be kind enough to provide input on the apoc 'path' methods in case I missed something.

I tried to do this in pure cypher with the following query:

match(start:N1{id:1})
match(end:N2{id:2})
match p=(start)-[]->(:L1)-[:R1*0..]->(:L1)-[]->(:L2)-[:R1|R2*0..]-(:L2)-[]->(end)
return p

I did not know what relationship types you want between 1) the start node and the first L1 node, 2) the relationship type between the transition from L1 to L2 nodes, and 3) the relationship between the final L2 and the end node. The query does not specify these three relationship types. We can update the query if you do have such restrictions. 

Of course, substitute your criteria for the start and end nodes.

Let me know how it works and we can try to make adjustments. 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

I reviewed the apoc 'path' procedures and did not find one that allows to specify a pattern of nodes. Maybe @bennu_neo will be kind enough to provide input on the apoc 'path' methods in case I missed something.

I tried to do this in pure cypher with the following query:

match(start:N1{id:1})
match(end:N2{id:2})
match p=(start)-[]->(:L1)-[:R1*0..]->(:L1)-[]->(:L2)-[:R1|R2*0..]-(:L2)-[]->(end)
return p

I did not know what relationship types you want between 1) the start node and the first L1 node, 2) the relationship type between the transition from L1 to L2 nodes, and 3) the relationship between the final L2 and the end node. The query does not specify these three relationship types. We can update the query if you do have such restrictions. 

Of course, substitute your criteria for the start and end nodes.

Let me know how it works and we can try to make adjustments. 

Thanks! It works!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.