Get friend posts with comments and likes

I'm new to neo4j and tried making a query that gets the following:

  • all stories of friends that haven't been seen by the user & aren't private
  • comments of friends on that story
  • likes of friends on that story

My query looks like this:

Match (P:User {id: 5})
Match (a:Story)-[BEL_TO]->(P2: User)-[:FRI]-(P)
Where not (P)-[:SEEN]->(a) AND NOT a.priv = "bf"
optional Match (a)<-[:LIKE]-(P3: User)-[:FRI]-(P)
with a, COLLECT({id:, name:}) as flikes
optional Match (a)<-[:COM_ON]-(com:Com)-[:BEL_TO]->(P4: User)-[:FRI]-(P) 
RETURN as id, flikes, COLLECT({id:, name:, comment: com}) as fcomments

When I run this query, I get the results I want, but I tested the query in the following scenario:

  • 1000 "new" posts (no relation seen)
  • 50 likes and comments on each post
  • user has 1000 friends

The query takes around 1500 ms

Is there a way to make this query more efficient/faster ?

Things I thought of:

  • adding an index (but don't know where)
  • using "join on a" (but gave errors in neo4j)

Thanks in advance.

Screenshot of profiler:

Hi @karel.debedts1

You can run the "create index" only once before your Cypher.

CREATE INDEX index_priv FOR (a:Story) ON (a.priv);