cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How can I add an OR to this MATCH statement?

geronimo4j
Graph Buddy

Hi I currently have this query (that works)

    MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
    WITH post, ('User' IN labels(something)) AS followingAuthor
    RETURN DISTINCT post
    ORDER BY post.date

It returns any time a user follows some unnamed node, and that node either wrote or was tagged in a post.

I'm now looking to also ADD any time a user posts something themselves. Like if a user wrote a post they wouldn't be following themselves, so it might not show. Any idea how to incorporate this second OR condition? I tried adding it to the MATCH or even creating a WHERE statement but am stuck! Thank you in advance!

7 REPLIES 7

Your query is too general, and seems to miss some labelling.
This can lead to a huge execution time and it's hard to understand what you really want to do here.

Maybe you can explain in English what you want to do and some Cypher expert will help you to write it.

That's a good idea!

Essentially I have these nodes:

Users (people)
Things
Posts

Users can follow Users or Things
Posts can tag Things (like if a post is about the movie Elf, then ElfPost could have Elf tagged).

Then if a user is trying to see all of their posts (typical social media style):

Then it should return any time a User follows either a Thing or a User, and that Thing or User has been tagged in or wrote a Post.

That's where the current MATCH comes in:
MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)

Where (this) is part of a User object, so you can assume (this) will be a User when run, and they follow either a User or a Thing (should I specify this Node type somehow? Currently it's just "something".)
Then that "something" has a relevant relationship to a Post.

This seems to work.

It doesn't, though, take into consideration that "this" (the User who the request is for) is the author to a post. So where (this)-[:WROTE]->(post:Post) is essentially what it's missing. I want to include those.

Is there any optimization I can make to the query and also include when the user WROTE the Post? Thank you!

MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
WHERE something.id <> this.id OR something.someProp <> this.someProp
Try this if it works?

I think this might exclude things when I want to include more.

I am trying to see when a user posts something themselves. So it’s a direct relationship between User and Post, instead of going User to User to Post, or User to Thing to Post.

ameyasoft
Graph Maven
Try this:

 MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
WHERE something.id <> this.id
(assuming id is property)

clem
Graph Steward

Have you considered the UNION operator? UNION - Neo4j Cypher Manual

MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
WITH post, ('User' IN labels(something)) AS followingAuthor
RETURN DISTINCT post

UNION
MATCH (this)-[:WROTE]->(post:Post)
RETURN DISTINCT post

and this looks interesting too

That seems to have done it! Thank you a million!