cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Filter Relationships

Hi Neo4j Community,

I hope its okay that i opened a new topic, but im stuck for 2 days.
So i got a Database with Persons as Nodes and each "Meeting" of each other i displayed as Relationsship with the property datetime.
Here's one extract from the database:

< CREATE (Tim:Person { name:"Tim Hank", born:1964, tel:"01762258478", street:"Hasenbergstraße 54", city:"Stuttgart" })
CREATE (Max:Person { name:"Max Müller", born:1984, tel:"01843537328", street:"Heusteigstraße 23", city:"Stuttgart" })
CREATE (Lisa:Person { name:"Lisa Brot", born:1994, tel:"0162355847", street:"Mühbenreuteweg 9", city:"Köln" })
CREATE (Marie:Person { name:"Marie Röthlingshöfer", born:1993, tel:"017234738", street:"Taubenstraße 83", city:"Ravensburg" })
CREATE (Luise:Person { name:"Luise Kaiser", born:1950, tel:"0178443278", street:"Hauptstätterstraße 42", city:"Weingarten" })
CREATE (Frank:Person { name:"Frank Michel", born:2001, tel:"01523263258", street:"Am Lerchenrain 4", city:"Köln" })
CREATE (Cedric:Person { name:"Cedric Luther", born:1970, tel:"0153326378", street:"Langestraße 64", city:"Stuttgart" })
CREATE (Arne:Person { name:"Arne Friedrich", born:1985, tel:"0175849264", street:"Am Lerchenrain 4", city:"Köln" })
CREATE (Alena:Person { name:"Alena Luther", born:1980, tel:"017262144378", street:"Frauenstraße 73", city:"Weingarten" })
CREATE (Alice:Person { name:"Alice Hauser", born:1985, tel:"0176849264", street:"Dorotheenstraße 83", city:"Ravensburg" })
/>`

The Relations:
<CREATE (p2)-[:MET_ON { datetime:("2020-10-21T18:40:32")}]->(p3)

CREATE (p3)-[:MET_ON { datetime:("2020-10-21T18:40:32")}]->(p4)

CREATE (p5)-[:MET_ON { datetime:("2020-10-21T18:40:32")}]->(p6)

CREATE (p7)-[:MET_ON { datetime:("2020-10-21T19:40:32")}]->(p8)

CREATE (p9)-[:MET_ON { datetime:("2020-10-21T19:40:32")}]->(p2)

CREATE (p4)-[:MET_ON { datetime:("2020-10-21T19:40:32")}]->(p6)

CREATE (p3)-[:MET_ON { datetime:("2020-10-21T17:40:32")}]->(p8)

CREATE (p6)-[:MET_ON { datetime:("2020-10-21T17:40:32")}]->(p)

CREATE (p7)-[:MET_ON { datetime:("2020-10-21T17:40:32")}]->(p7)

CREATE (p4)-[:MET_ON { datetime:("2020-10-21T16:40:32")}]->(p2) />

The current Command to filter the relations:

< MATCH p=(me:Person)-[:MET_ON*1..3]-(remote_friend)
WITH *, relationships (p) AS q
WHERE me.name = 'Cedric Luther' AND q[0].datetime >= ("2020-10-21T19:30:00")
Return p, q, remote_friend.name />

So I want to filter all Meetings after the Datetime, e.g. every Person who has met "Person XY" after a specific Datetime should only be displayed.

USE CASE:
I want to build a basic tracker for the Covid Case.
Person 1 --> (met at 19.40) --> Person 2 --> (met at 18.30) --> Person 3
So i want only the first relation between Person 1 and Person 2 to be displayed, but currently i can only check the first Date if its later than 19.30.
How can i arrange it to check every Relation and stop once the datetime is checked.
If we assume Person 1 got infected at 19.30.
Person 1 met Person 2 at 19.40, so Person 2 is potentially affected. So every other Person who met Person 2 is also at risk. But Person 2 met Person 3 at 18.30. So Person 3 is at no Risk.

I hope somebody can help me, or show me a similiar problem

Did i mess up already, when i built the database!

Thanks a lot !

1 REPLY 1

You are on the right track here. But you need list/pattern comprehensions on relationships(p) as q

Docs: https://neo4j.com/docs/cypher-manual/current/syntax/lists/#cypher-pattern-comprehension

My syntax isn't going to be exact because I'm doing it off the cuff, but it's going to be something like this:

I've re-arranged some other things, might do it like this:

MATCH p=(me:Person { name: "Cedric Luther"})-[:MET_ON*1..3]-(remote_friend)
WITH p, me, remote_friend, 
      [rel in relationships (p) WHERE rel.datetime >= "2020-10-21T19:30:00"] as q
WHERE length(q) > 0
Return p, q, remote_friend.name

What's going on here?

  • The list comprehension reduces relationships(p) into a list q where only those rels with a certain datetime are included
  • The final WHERE throws out all results where none of the relationships matched.