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.

How to create relationships between 2 nodes with similar node types in neo4j based on the timestamp(a similar property)?

I am trying to populate the neo4j database from Kafka using a connector so the person node will constantly be created. I am trying to create a relationship between Person nodes if the timestamp is the same.

MERGE (n:Person{age: event.age, gender:event.gender, glasses:event.glasses }) 
ON CREATE SET n.id=event.id 
WITH (n:Person) as n1, (n:Person) as n2 WHERE n1.event.time = n2.event.time AND n1<>n2 
MERGE (n1)-[:was_at {timestamp: event.Time}] –(n2)

The timestamp sent over from my json file has the following format : 2020-10-08T10:29

Thanks for your help in advance!

6 REPLIES 6

Hi @james.irvin,
I am no expert but if this was my problem, I would split it into 2 queries.

  1. Create the nodes
  2. Create the relationships

To create the relationships I would use the query below

MATCH (a:Person)
MATCH (b:Person)
WHERE b.Time = a.Time AND NOT id(b) = id(a)
MERGE (a)-[:WAS_AT {timestamp: b.time}]->(b)
RETURN a, b

Hope this helps you

Hi, thank you for the suggestion.

Can I know what does a and b represent because person nodes are constantly being inputed in.

The a variable - represents the person node found in the database
The b variable - represents another person node that has the same time property as the a variable found

However the above query doesn't work well after some time as the number of person nodes it is finding keeps increasing as you add more data. If the data your adding has the time property that keeps increasing then you can filter out the old person nodes. Query shown below:

MATCH (a:Person)
WHERE a.Time > 'some value'
MATCH (b:Person)
WHERE b.Time = a.Time AND NOT id(b) = id(a)
MERGE (a)-[:WAS_AT {timestamp: b.time}]->(b)
RETURN a, b

asperlinga
Graph Buddy

...and remember to create constraint index 🙂

ameyasoft
Graph Maven
The Person node has age, gender, glasses, id properties as per your MERGE statement.

Under WHERE you are referring to n1.event.time . event.time is not a property of Person node. It's a value you are getting from Kafka. This should have raised an error by Neo4j. May be there is a typo!.

please check your event.time property for person node