Matt Way
Matt Way

Reputation: 33189

Neo4j/Cypher Check for existing relationship on each node in linked list

I have a cypher which returns a list of items created by a given user (which works fine):

MATCH (author:User)-[:ITEM_LIST*1..10]->item
WHERE author.username='1'
RETURN item

What I want now though, is to return a list of authored items, but to also know if a particular user has liked any of these items. I have tried the cypher below, but it doesn't work:

START user=node(1)
MATCH (author:User)-[:ITEM_LIST*1..10]->item<-[like?:LIKES]-user
WHERE author.username='1'
RETURN item, (like IS NOT NULL) as likes

I read the cypher above as returning all items, and optionally any likes between the items and the given user if they exist. Unfortunately the ? makes no difference to the results, and it appears as though the item<-[like?:LIKES]-user is a requirement.

What would I need to do, to achieve the desired results?

EDIT:

I realised that I can do this by using WITH item and then checking for each optional relationship. Will this cause any performance issues? If so, is there a better way?

Upvotes: 1

Views: 672

Answers (1)

Mohamed Ismail Mansour
Mohamed Ismail Mansour

Reputation: 1053

Using WITH syntax is similar to RETURN. It separates query parts explicitly, allowing you to declare which identifiers to carry over to the next part..

I'm Using it in many situations and I don't note any performance issue..

Also, you may create sample of the graph here, so we can help..

Upvotes: 1

Related Questions