Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
01-10-2021 07:52 PM
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!
01-10-2021 08:21 PM
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.
01-10-2021 09:17 PM
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!
01-10-2021 09:28 PM
MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
WHERE something.id <> this.id OR something.someProp <> this.someProp
Try this if it works?
01-11-2021 02:49 PM
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.
01-10-2021 08:52 PM
Try this:
MATCH (this)-[:FOLLOWS]->(something)-[:WROTE|TAGGED]-(post:Post)
WHERE something.id <> this.id
(assuming id is property)
01-11-2021 05:07 PM
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
01-11-2021 08:15 PM
That seems to have done it! Thank you a million!
All the sessions of the conference are now available online