drgraduss
drgraduss

Reputation: 381

Neo4j/Cypher default rel-type chain ordering issue

This is the follow up to original question regarding paginating result set for large sub-graph.

Neo4j/Cypher effective pagination with order by over large sub-graph

To recap the node-rel structure:

(:User)-[:FOLLOWS {timestamp}]->(:User)

As suggested by @michael-hanger order by clause has been removed as rel-type chain should be returned in sequential order following timestamp with most recent followers on top.

MATCH (u:User {Id:{id}})<-[f:FOLLOWS]-(follower)
WHERE f.timestamp <= {timestamp}
RETURN follower
LIMIT 100

The problem is that in general case this doesn't work.

What I found is if :User node has 50 or less :FOLLOWER relationships everything works fine. Followers always returned ordered by timestamp with most recent on top. But in case you have more then 50 :FOLLOWER relationships last 50 followers always returned inverted with oldest on top.

i.e. if :User has 100 followers and you query them as above, you will get list where first 50 ordered from newest to oldest and last 50 from oldest to newest. in general you have such ordering in any case where number of followers is more than 50.

Could anyone advise on this behaviour.

Upvotes: 1

Views: 130

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

By default any node with more than 50 relationship is considered being a "dense nodes". Normal nodes organize their relationships in a single chain regardless of their type and direction.

A dense node instead manages a separate chain per relationship type and direction.

What you're seeing is the effect of a node becoming a dense node and therefore a reorganization of the internal structure. The treshold value for this - defaulting to 50 - can be configured using dense_node_treshold config option.

Depending on your data model you can consider increasing that value.

Another approach not relying on implementation details: Refactor the FOLLOWS relationships into nodes. The :Follow nodes have one relationship to the follower, one to the followee and a third one forms a linked list based on timestamps of all :Follow nodes for the give person.

Upvotes: 1

Related Questions