简体   繁体   中英

Neo4j linked list - multiple nodes

I'm working on understanding how to use linked lists to improve performance and create activity feeds on Neo4j.. Still working on learning Cypher, so I have a question.. I've found some examples of linked lists, but I need lists with bigger examples to finally put all the pieces together in my head..

I've used this code from grepcode and have found it to be more helpful than the example in the Neo4j manual. Yet I'm still a bit confused.. Can someone modify it to have say seven nodes with seven items in the linked list, and then insert a node on the front of it?

Yea, I'm trying to put the latest status update on the top of the linked list. This example doesn't really do that, but it's close.. so looking for some mods.. No, I'm not really coding yet, still trying to master Cypher first - will continue to study it for the next two weeks... Have the Ruby on Rails side working .. just need to understand linked lists used with Cypher/Neo a bit better.


CREATE zero={name:0,value:0}, two={value:2,name:2}, zero-[:LINK]->two-[:LINK]->zero

==== zero ====

MATCH zero-[:LINK*0..]->before,
after-[:LINK*0..]->zero,
before-[old:LINK]->after
WHERE before.value? <= 1 AND
1 <= after.value?
CREATE newValue={name:1,value : 1},
before-[:LINK]->newValue,
newValue-[:LINK]->after
DELETE old
==== zero ====
MATCH p = zero-[:LINK*1..]->zero
RETURN length(p) as list_length

What I'm trying to do in my mind is understand the before after and zero data sets - I almost have it, but want to see how it's done on a set with more than two starting nodes so as to clear up any confusion

Thank you!

The node in front is special as it doesn't have a incoming link relationship. Usually you also keep the connection to the head node somewhere, so this is about replacing this link to the head node and moving the head node one step further away. Something like this:

start user=node:node_auto_index(user="me")
match user-[old:MESSAGES]->head
delete old
create new_heads = { title: "Title", date : 2348972389, text: "Text" },
 user-[:MESSAGES]->new_head-[:LINK]->head

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM