retrieving unique posts from distinct users every time in social newsfeed neo4j - neo4j

Retrieving unique posts every time in social newsfeed neo4j .
Right now i am using this script :-
MATCH (u:Users {user_id:140}),(p:Posts)-[:CREATED_BY]->(pu:Users)
WHERE
(p)-[:CREATED_BY]->(u) OR
(p:PUBLIC AND (u)-[:FOLLOW]->(pu)) OR
(p:PRIVATE AND (p)-[:SHARED_WITH]->(u))
OPTIONAL MATCH (p)-[:POST_MEDIA]->(f)
OPTIONAL MATCH (p)-[:COMMENT]->(c)<-[:COMMENT]-(u3)
RETURN
{user_id:pu.user_id,
firstname:pu.firstname,
lastname:pu.lastname,
profile_photo:pu.profile_photo,
username:pu.username} as pu,
p,
collect({user_id:u3.user_id,
profile_photo:u3.profile_photo,
text:c.text}) as comment,
collect(f) as file
ORDER BY p.post_id DESC LIMIT 25
The problem is that this script may return multiple post from a single user
and always show same posts.

Related

How can I grab a date in Neo4j GraphQL that is either from the relationship or from the node?

Hi the below is within the context of Neo4j GraphQL (so I need to ensure I'm returning nodes here, rather than Maps or anything, I think).
I have nodes Users and Posts where users can write or repost posts. Essentially posts have created_date fields but when a user "reposts" that post, I would like to get the time of that repost.
The typical relationships look like this:
(u:User)-[r:WROTE]->(post:Post)
Where posts have created_date fields. When a user reposts it, the underlying post doesn't change, they just add a REPOSTED relationship to that post. Like this:
(u:User)-[r:REPOSTED]->(post2:Post)
And when I want to look at a user's posts, I want to grab any that they've written or reposted and sort them in the order of either the post.created_date if they WROTE the post or REPOSTED time if they reposted it.
I have no idea what I should be doing here, so I attempted something like this but it isn't editing the repost_date in time (it doesn't return the correct result).
MATCH (u:User)-[r:WROTE|REPOSTED]->(post:Post)
WITH (CASE WHEN r.created_date IS NOT NULL THEN r.created_date ELSE post.date END) as repost_date, post
SET post.repost_date = repost_date
RETURN post, repost_date
ORDER BY repost_date DESC
LIMIT 10
Is there another way to grab and return both dates (when both exist, i.e. it's a REPOST)?
Thank you in advance!
There are a couple ways to achieve what you want. Probably the simplest is:
MATCH (u:User)-[r:WROTE|REPOSTED]->(post:Post)
WITH post, coalesce(r.created_date, post.created_date) AS date
ORDER BY date DESC
LIMIT 10
RETURN post
You could also use a subquery UNION and then use post-filtering
MATCH (u:User)
CALL {
MATCH (u)-[:WROTE]->(p:Post)
RETURN p, p.created_date as date
UNION
MATCH (u)-[r:REPOSTED]->(p:Post)
RETURN p, r.created_date as date
}
WITH p, date
ORDER BY date DESC
LIMIT 10
RETURN p
Third option is to use CASE statement
MATCH (u:User)-[r:WROTE|REPOSTED]->(post:Post)
WITH post, CASE WHEN r:REPOSTED THEN r.created_date ELSE post.created_date END AS date
ORDER BY date DESC
LIMIT 10
RETURN post
You can use the PROFILE clause to see what is best

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
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

neo4j - Return single instance of node - querying by property?

I am building a social network that has a specialized audience.
Users are related to each other by three primary relationship types.
[:FRIENDS]->(:USER),
[:WORKS_AT]->(:COMPANY),
[:WORKED_AT]->(:COMPANY),
[:FOLLOWS].
When working through a search scenario (a user wants to find another user), I've given each relationship a "priority" (so to speak).
For example, if a user wants to find another user named "Bart Simpson" - first, we will check co-worker relationships ([:WORKS_AT],[:WORKED_AT]). I've assigned those relationships a priority of 1. That way, "Bart Simpson" who works with me will appear in the search results before "Bart Simpson" - who lives hundreds of miles away in Springfield.
The second priority is [:FRIENDS]->(:USER). Do any of my friends have a friend named "Bart Simpson?" Priority #2.
The last priority is a global search. I don't have any co-workers named "Bart Simpson", my friends don't have any friends named "Bart Simpson" - but I met Bart at a conference, and I want to "friend" him. So, I've added a "Global" search. Find any users named "Bart Simpson".
So far, this is my Cypher:
optional match (u:USER {id:'1'})-[:WORKS_AT|:WORKED_AT]-(w:COMPANY)-[r]-(f:USER)
with collect(f{.*, priority:1,relationship:r.title,type:type(r)}) as user
optional match (u:USER {id: '1'})-[:FRIENDS]-(:USER)-[r:FRIENDS]-(f:USER)
with user + collect(f{.*, priority:2,relationship:r.title,type:type(r)}) as user
optional match (f:USER)
where f.id <> '1'
with user + collect(f{.*, priority:3,relationship:'',type:''}) as user
unwind user as users
with users as user
where toLower(user.last_name) STARTS WITH toLower('Sc') OR toLower(user.first_name) STARTS WITH toLower('Sc')
return distinct user
This is fantastic - however, a user could work at the same company, as well as
be friends, as well as appear in the global search. So - we have the potential for three (or more) "copies" of the same user - with different relationship attributes. The relationship attributes are important because in the app, they provide important context to the search. "Bart Simpson - Works at XYZ Company."
So what I'm really looking for is the ability to either return the user record with the highest priority - and do that based on the "ID" field. If that doesn't work, I could see a situation where we try to update the property of a node. So, when the query hits the priority 2 search, if there is already a user in the collection with the same "ID", it just appends the P2 relationship type to the record. Either is fine with me.
I'm open to suggestions and listening!
So, I've made some progress!
MATCH
(subject:USER {id:'1'})
MATCH
(subject)-[:WORKS_AT|:WORKED_AT]-(w:COMPANY)-[r]-(f1:USER)
WHERE
toLower(f1.last_name) STARTS WITH toLower('Sc') or
toLower(f1.first_name) STARTS WITH toLower('Sc')
WITH
COLLECT(f1.id) AS userIds,
COLLECT(f1{.*,priority:1,rType:type(r), title:r.title, detail:w.name}) AS users
OPTIONAL MATCH
(subject)-[:FRIEND]-(fw:USER)-[r:FRIEND]-(f2:USER)
WHERE
NOT(f2.id in userIds) AND
(
toLower(f2.last_name) STARTS WITH toLower('Sc') or
toLower(f2.first_name) STARTS WITH toLower('Sc')
)
WITH
users + COLLECT(f2{.*,priority:2,rType:"FRIEND", title:"Friends with " + fw.first_name + " " + fw.last_name, detail:''}) AS users,
userIds + collect(f2.id) AS userIds
OPTIONAL MATCH
(f3:USER)
WHERE
NOT(f3.id in userIds) AND
(
toLower(f3.last_name) starts with toLower('Sc') OR
toLower(f3.first_name) starts with toLower('Sc')
)
WITH
users + COLLECT(f3{.*,priority:3,rType:"GLOBAL", title:"", detail:''}) AS users
RETURN
users
The query has evolved a bit. Essentially, at the first stage, we collect the userIds of the items that were returned. At each subsequent stage, the results returned are compared against the running list of ids. If the id of the result is already in the list of ids, it is filtered out - thus ensuring a unique id in the set.
This is working - and for now, I'm going to run with it. Is this the most efficient query, or is there a better way to deal with this scenario?

Return all users given user chats with and the latest message in conversation

my relationships look like this
A-[:CHATS_WITH]->B - denotes that the user have sent at least 1 mesg to the other user
then messages
A-[:FROM]->message-[:SENT_TO]->B
and vice versa
B-[:FROM]->message-[:SENT_TO]->A
and so on
now i would like to select all users a given user chats with together with the latest message between the two.
for now i have managed to get all messages between two users with this query
MATCH (me:user)-[:CHATS_WITH]->(other:user) WHERE me.nick = 'bazo'
WITH me, other
MATCH me-[:FROM|:SENT_TO]-(m:message)-[:FROM|:SENT_TO]-other
RETURN other,m ORDER BY m.timestamp DESC
how can I return just the latest message for each conversation?
Taking what you already have do you just want to tag LIMIT 1 to the end of the query?
The preferential way in a graph store is to manually manage a linked list to model the interaction stream in which case you'd just select the head or tail of the list. This is because you are playing to the graphs strengths (traversal) rather than reading data out of every Message node.
EDIT - Last message to each distinct contact.
I think you'll have to collect all the messages into an ordered collection and then return the head, but this sounds like it get get very slow if you have many friends/messages.
MATCH (me:user)-[:CHATS_WITH]->(other:user) WHERE me.nick = 'bazo'
WITH me, other
MATCH me-[:FROM|:SENT_TO]-(m:message)-[:FROM|:SENT_TO]-other
WITH other, m
ORDER BY m.timestamp DESC
RETURN other, HEAD(COLLECT(m))
See: Neo Linked Lists and Neo Modelling a Newsfeed.

Cypher: Query to Combine Collection for InFeed Ads (Show 1 Ad Every X Amount of Posts)?

I am trying to build a Cypher query which allows me to build in-feed ads:
An example is how on the Facebook Mobile App an ad appears inside the feed for every X numbers of posts (Lets say 1 ad for every 5 posts on same feed).
So far I have this: "MATCH (P:Post) (A:Ad) return P,A"
Post would be the User's Posts.
Ad would be ads to put inside a User's feed.
I'm able to get both collections, but am lost on how to combine this to create an effect similar to in-Feed apps.
What is your actual use-case?
Do you have a feed of Ads somewhere and want to merge it with user's posts?
How do you model ad-feeds and post-feeds?
You probably also have Ad-Publishers, Categories etc? Same for posts?
So something like this:
MATCH (u:User {login:"john"})-[:POSTED]->(p:Post)
WITH p
LIMIT 20
MATCH (:Publisher {id:"3829472"})-[:PUBLISHED]->(ad:Ad)<-[:AD_CATEGORY]-(c)-[:POST_CATEGORY]->(p)
RETURN p,case when random() < 0.2 then ad else null end
You should probably look into graph modeling.
For actual cypher questions check the manual and the refcard.

Resources