Hi, I'm trying to build v1 of a query (pagination and other stuff will come later) that will generate a social media timeline for a project I'm working on.
Here is the query I currently have. Right now, I'm able to get the most recent user that liked the site. However, like the title says, I want the most recent 3 users.
I'm having trouble wrapping my head around how collect/unwind work. Specifically, how does the apoc.agg.maxItems know to only look at the LIKED_SITE relationships that are tied to the current site it's looking at? If I do a collect on the ls inside that WITH, how does it know to group them based off the site they're pointing to? I thought I understood how this worked, but as soon as I UNWIND the collections to perform operations on them, I can't collect them back into lists for each site.
Sorry if this is confusing... Just having a hard time wrapping my head around how neo4j/cypher work. Would really appreciate just any general advice as well.
MATCH (u:User {id: $cypherParams.userId})-[:IS]->(pu:PublicUser)-[:FOLLOWS]->(following_pu:PublicUser)-[:HAD_SESSION]->(sesh:Session)-[ls:LIKED_SITE]->(site:Site)
WITH
site,
duration.inSeconds(max(ls.lastLikedOn), datetime()) AS dur,
max(ls.lastLikedOn) AS lastLikedOn,
apoc.agg.maxItems(ls, ls.lastLikedOn, 1) as most_recent_LIKED_SITE
MATCH (pu:PublicUser)-[:HAD_SESSION]->(:Session)-[:LIKED_SITE {id: most_recent_LIKED_SITE.items[0].id}]->(:Site)
RETURN {
url: site.url,
siteName: site.siteName,
title: site.title,
description: site.description,
image: site.image,
lastLikedBy: pu.username,
lastLikedOn: lastLikedOn,
secondsSinceLastLike: dur.seconds
} ORDER BY dur
PS: This will generate the same dataset that I have
CREATE (pu1:PublicUser) set pu1.username = "1"
CREATE (u1)-[:IS]->(pu1)
CREATE (u2:User) SET u2.username = "2", u2.id = randomUUID()
CREATE (pu2:PublicUser) set pu2.username = "2"
CREATE (u2)-[:IS]->(pu2)
CREATE (u3:User) SET u3.username = "3", u2.id = randomUUID()
CREATE (pu3:PublicUser) set pu3.username = "3"
CREATE (u3)-[:IS]->(pu3)
CREATE (u4:User) SET u4.username = "4", u4.id = randomUUID()
CREATE (pu4:PublicUser) set pu4.username = "4"
CREATE (u4)-[:IS]->(pu4)
CREATE (u5:User) SET u5.username = "5", u5.id = randomUUID()
CREATE (pu5:PublicUser) set pu5.username = "5"
CREATE (u5)-[:IS]->(pu5)
CREATE (jim:User) SET jim.username = "jim", jim.id = randomUUID()
CREATE (pujim:PublicUser) set pujim.username = "jim"
CREATE (jim)-[:IS]->(pujim)
with u5 as poop
MATCH (pu:PublicUser {username: "1"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "twitter.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:12, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
WITH pu as poop
MATCH (pu:PublicUser {username: "2"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "twitter.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:13, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
WITH pu as poop
MATCH (pu:PublicUser {username: "3"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "twitter.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:14, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
WITH pu as poop
MATCH (pu:PublicUser {username: "3"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "nastyboys.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:14, minute:51, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
WITH pu as poop
MATCH (pu:PublicUser {username: "4"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "twitter.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:15, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
WITH pu as poop
MATCH (pu:PublicUser {username: "4"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "pornhub.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:16, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
with pu as poop
MATCH (pu:PublicUser {username: "5"})
MERGE (pu)-[:HAD_SESSION]->(sesh:Session {date: date({year: 2017, month: 11, day: 11})})
MERGE (site:Site {url: "twitter.com"})
MERGE (sesh)-[r:LIKED_SITE]->(site)
ON CREATE
SET
r.id = randomUUID(),
r.timesLiked = 1,
r.lastLikedOn = datetime({year:2017, month:11, day:11, hour:11, minute:31, second:14, nanosecond: 645876123, timezone: '+01:00'})
ON MATCH
SET
r.timesLiked = r.timesLiked + 1,
r.lastLikedOn = datetime()
return pu as poop
MATCH (pu:PublicUser {username: "jim"})
WITH pu
MATCH(u1:PublicUser{ username: "1"})
MERGE (pu)-[:FOLLOWS]->(u1)
WITH pu
MATCH(u2:PublicUser{ username: "2"})
MERGE (pu)-[:FOLLOWS]->(u2)
WITH pu
MATCH(u3:PublicUser{ username: "3"})
MERGE (pu)-[:FOLLOWS]->(u3)
WITH pu
MATCH(u4:PublicUser{ username: "4"})
MERGE (pu)-[:FOLLOWS]->(u4)
WITH pu
MATCH(u5:PublicUser{ username: "5"})
MERGE (pu)-[:FOLLOWS]->(u5)
WITH pu
return pu
type or paste code here