Saturday, April 22, 2017

How to create a linked list in neo4j?

Having a "Post" nodes that have "FILED_IN" relations to "Topic" model. We can retrieve all posts filed under the Topic using;

MATCH (p:Post)-[FILED_IN]->(t:Topic) return p

But what if we need to retrieve next post. It will be convenient to have a relation to the next post. We can add this relation anytime using the following Cypher.

MATCH (p:post)-[FILED_IN]->(t:Topic)
WITH p
ORDER BY p.timestamp
WITH COLLECT(p) AS posts
UNWIND RANGE(0,LENGTH(posts) - 2) as idx 
WITH posts[idx] AS p1, posts[idx+1] AS p2
MERGE (p1)-[:NEXT]->(p2)
 

 
  

No comments:

Fun With Haxl (Part 1)

This is a blog-post version of a talk I recently gave at the Haskell eXchange 2015. The video of the talk is here, but there were a lot of...