Matt Way
Matt Way

Reputation: 33179

neo4j cypher (optional relationships on null endpoints)

I have simplified my complex cypher down its core issue.

START focus=node(2), user=node(20) 
MATCH focus-[:USER_FEED_LIST*1..15]->pointer-[:POINTER]->item,
item-[l?:NOTICE_LINK]->link<-[lu?:POST_UPVOTE]-user
RETURN link, ID(item) as item_id, item, l, lu

Basically what I am trying to achieve is as follows:

  1. return each item in the linked list
  2. for each item, return l and link if they exist, or null if not
  3. if link is not null, return lu and user if they exist, or null if not

The problem I am having is with 3. as the third line of the query is really saying return any link that satisfies either of the given relationships.

So for example, if my linked list chained 10 items, I need the query to always return 10 rows (null for l, link and lu if they don't exist).

Upvotes: 0

Views: 530

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

Probably you want to go with path expressions and not optional relationships? Those path expressions return a collection of paths. You can use extract / filter to extract certain parts of those.

START focus=node(2), user=node(20) 
MATCH focus-[:USER_FEED_LIST*1..15]->pointer-[:POINTER]->item,
RETURN ID(item) as item_id, item, (item-[:NOTICE_LINK]->link<-[:POST_UPVOTE]-user) 

Upvotes: 0

Related Questions