Is it possible to filter nodes on a specific level within a path


(Michael Horak) #1

Hello,
I would like to ask if there is a way to filter a specific level in a path.

eg. MATCH p=(n {name:'John'})-[:KNOWS]->(level 1)-[:ADDRESS]->(level 2)-[:COUNTRY]->(level 3) RETURN p;

I would like to filter the level 1 and return only the level 2, level 3 (all the following levels).

thank you


(Benoit Simard) #2

Hi Michael,

Why are you defining a path if you don't want to use it ?

From what I understand, your query can be written like that :

MATCH (n {name:'John'})-[:KNOWS]->(lvl1)-[:ADDRESS]->(lvl2)-[:COUNTRY]->(lvl3) 
WHERE lvl1.value = 'XXXX'  / /your level 1 filter
RETURN lvl2, lvl3 // return only the level 2, level 3

Cheers


(Michael Horak) #3

Hello,

The problem is that this query is dynamically generated so I don't know in advance which level should be filtered and on which value I should filter (defined by a user).

I want to use a path because I need to return all nodes and relations from the given pattern, except a given level so I could explicitly define a query:

MATCH (n {name:'John'})-[r1:KNOWS]->(level 1)-[r2:ADDRESS]->(level 2)-[r3:COUNTRY]->(level 3) RETURN r2, level2, r3, level3;

but I would like to ask if there is a way to do the same thing while using the path (return only path) because it simplifies the result parsing for me.

Thank you,
Michael


(Benoit Simard) #4

Re,

It's possible to do, but you can have some bad impact on the performances :


MATCH p=(n {name:'John'})-[:KNOWS]->(level 1)-[:ADDRESS]->(level 2)-[:COUNTRY]->(level 3) 
WHERE head(tail(nodes(p))).value = 'XXXX' // your level 1 filter
RETURN tail(tail(nodes(p))), tail(tail(relationships(p))) // return all the level except the root and lvl1

In the WHERE clause, Neo4j is working on an array, so it can't use any indexes.