Nourish Cherish
Nourish Cherish

Reputation: 1

Neo4j Cypher query to fetch posts created by followers

I am creating an app kind of like Facebook. It is an app where people can share products and collections of products. In the "create a post" popup, people can either select a product or a collection (group of products but consider it as a single object) or just text to create a post. I need to fetch the posts created by my followers.

Each post will have a property of type PRODUCT, COLLECTION, OR TEXT to indicate what type of post it is.

In my neo4j DB, there is a Post object, product object, collection object and user object. When you create a post, relations will be created between them.

(post)-[:CREATED_BY]->(USER)
(post{type:"PRODUCT"})-[:INCLUDES]->(product)
(post{type:"COLLECTION})-[:INCLUDES]->(collection)

This is what I tried to get the posts of type "PRODUCT". IT shows an error. but just to give a basic idea of our properties.


   MATCH (user:User{lastName: "mylastname"})-[:FOLLOWS {status: "accepted"}]->(following) WITH following 
   OPTIONAL MATCH (post:Post {type: "PRODUCT"})-[r:CREATED_BY]->(following) WITH post,user, r OPTIONAL 
   MATCH 
   (post)-[:INCLUDES]->(product:Product) WITH COLLECT({post:post, datetime: r.datetime, 
   type:"PRODUCT",product:product user: following}) as productPosts
   UNWIND productPosts AS row
   RETURN row
   ORDER BY row.datetime DESC
   SKIP 0
   LIMIT 10

Upvotes: 0

Views: 53

Answers (1)

cybersam
cybersam

Reputation: 67019

Your WITH clauses are not specifying all the variables that need to be carried forward to the remainder of the query. Also, there has at least one typo (a missing comma).

In fact, your query does not even need any WITH clauses. Nor does it need to COLLECT a list only to immediately UNWIND it.

This query should work better:

MATCH (user:User{lastName: "mylastname"})-[:FOLLOWS {status: "accepted"}]->(following)
OPTIONAL MATCH (post:Post {type: "PRODUCT"})-[r:CREATED_BY]->(following)
OPTIONAL MATCH (post)-[:INCLUDES]->(product:Product)
RETURN {post:post, datetime: r.datetime, type:"PRODUCT", product:product, user: following} AS row
ORDER BY row.datetime DESC
LIMIT 10

Upvotes: 1

Related Questions