Cypher Querie for compare two node attribute recurcively


(Col One) #1

I try to find a right queries, at moment from the browser. I have a simple graph with node dependencies like parent -[:depend]-> child, each node have a modification_date attribute. From the last child I want to return all the "younger" parent in relation to their direct children, it's a real headache for me =].
Here a simple schema:

I notice the comparison result under connections.

From the 'node10' the expected result should be : node09, node06, node08.

Thanks a lot for your help.

(Col One) #2

At moment I am stuck with this kind of queries:

match (A:Node)-[:DEPEND*..4]->(AC:Node {name:'node10'}) where A.modification_date>AC.modification_date return A

the problem is that it compare modification date with last node (node10) not with its direct child.

(Andrew Bowman) #3

At the moment I can't think of a good query to use that would evaluate dynamically during traversal, but here's a query that will filter afterwards:

MATCH path = (AC:Node {name:10})<-[:DEPEND*..4]-(A:Node)
WITH A, [node in nodes(path) | node.modif_date] as dates
WHERE all(i in range(0, size(dates) - 2) WHERE dates[i] < dates[i + 1])

(Col One) #4

Thanks a lot !
Just I don't understand the - 2 in range(0, size(dates) - 2).

(Col One) #5

I understand the logi behind it, but in fact in my case is little bit more complicated :

as we can see in this graph, the modification date is hold by a [:CURRENT] relationship attribute connect to a (M:Media) node.

With your previous answer I tried to find a result... but nothing, I still don't master well enough...

Your help will be marvelous !

(Andrew Bowman) #6

This one is a bit tricky, we need to use a nested pattern comprehension within the list comprehension (and make sure to project out the first element from that pattern comprehension):

MATCH path = (AC:Node {name:10})<-[:DEPEND*..4]-(A:Node)
WITH A, [node in nodes(path) | [(node)-[r:CURRENT]->() | r.modification_date][0]] as dates
WHERE all(i in range(0, size(dates) - 2) WHERE dates[i] < dates[i + 1])

(Col One) #7

Thanks a lot, Cypher is very powerful ! However my real case is still a bit tricky because r.modification_date is not always present ... Therefore I have some null values in dates lists : [null, 1544706574.1611595, null, 1543230042.1031768]. I tried to filter like that in the nested list but I still have those null :

[(node)-[r:CURRENT]->() where not r.modification_date is null | r.modification_date]

(Col One) #8

@andrew.bowman any idea why I keep those null event if they filter them ?
Thanks !!

(Andrew Bowman) #9

Which version of Neo4j and APOC are you using? The WHERE r.modification_date IS NOT NULL should have worked here.

(Col One) #10

Neo4j Browser version: 3.2.5
Neo4j Server version: 3.4.5(community)

And honestly I have no idea about APOC version, where I can find this information.

(Andrew Bowman) #11

You can use return apoc.version() to get the APOC version

(Col One) #12

Unknown function 'apoc.version' hum ... have I APOC ?

(Andrew Bowman) #13

My mistake, I confused this question with a different one. You aren't using APOC Procedures here, it's not relevant to your question. Please disregard the request for the APOC version.

(Andrew Bowman) #14

You may want to try upgrading your version, I know there was a bug affecting the planning of nested pattern comprehensions in 3.4.5 and below. Try upgrading to 3.4.10 (or 3.4.11) and see if you get expected results.

(Col One) #15

Hi again @andrew.bowman.
I upgraded neo4j 3.5.1, but unfortunately I still those null values.... what is weird that is :
[node in nodes(path) | [(node)-[r:CURRENT]->() where r.modification_date is null | r.modification_date][0]]
give me null only, which mean not is the problem... ?