cancel
Showing results for 
Search instead for 
Did you mean: 

How to access node, relationship in variable

Hi there, I have member and event node. I want to find out which event is paid the most attention by count with "register" and "bookmark" relationship so that I  start from these query

 

Match p1=(:Member)-[r1:REGISTER]->(e1:Event)
Match p2=(:Member)-[:BOOKMARK]->(:Event)
with collect(p1)+collect(p2) as events
 
I don't know how can we use "events" collection eg. count relationship,  get node in collection 
Cloud you give any advice or give better query ? 
 
Updated 
 I just want only event node which has most attention (count by "REGISTER" or "BOOKMARK" if member have both just count "REGISTER") I try apoc.coll.sort that throw errors 
Failed to invoke function `apoc.coll.sort`: Caused by: java.lang.ClassCastException: class java.util.HashMap cannot be cast to class java.lang.Comparable (java.util.HashMap and java.lang.Comparable are in module java.base of loader 'bootstrap')


 cloud you help improve this query ? or better solution ? 

Match p1=(m1:Member)-[r1:REGISTER]->(e1:Event)
With e1 as e1,count(r1) as num
with collect({event:e1,num:num}) as events

 

Match p2=(m2:Member)-[r2:BOOKMARK]->(e2:Event)
where NOT e2 IN events
with e2 as e2, count(r2) as num2, events as events
with collect({event:e2,num:num2})+collect(events) as events // order by num not sure how to sort it 
return events
4 REPLIES 4

ameyasoft
Graph Maven

Try this:

Match p1=(a:Member)-[:REGISTER]->(e1:Event)

with nodes(p1) as n1, relationships(p1) as rel
unwind n1 as n2
unwind rel as rels

 

Match p2=(b:Member)-[:BOOKMARK]->(c:Event)
where id(b) in id(n2) and id(c)in id(n2)

return p2

This should show you the Member and Event nodes with [:REGISTER] and [:BOOKMARK] relationships

glilienfield
Ninja
Ninja

It sounds like 'find out which event is paid the most attention' means determining how many members registered and bookmarked each event, so you can determine which is more active. If that is what you are looking for, try the following: 

Match (m:Member)-[:REGISTER]->(e:Event)
with e, count(*) as noOfMemberRegistrations, collect(m.name) as registeredMembers
Match (m:Member)-[:BOOKMARK]->(e)
with e, noOfMemberRegistrations, registeredMembers, count(*) as noOfMemberBookmarks, collect(m.name) as bookmarkedMembers
return e, noOfMemberRegistrations, registeredMembers, noOfMemberBookmarks, bookmarkedMembers, noOfMemberRegistrations + noOfMemberBookmarks as totalEventMembers
order by totalEventMembers desc

The query counts the total number of member registrations and bookmarks for each event, as well as collecting the names of the members that registered and bookmarked the event. It then lists the events by total count in descending order so you can see the events with the most 'attention.' 

@glilienfield 
ah I figure out, I just want only event node which has most attention (count by "REGISTER" or "BOOKMARK" if member have both just count "REGISTER") cloud you help improve this query ? or better solution ? 

Match p1=(m1:Member)-[r1:REGISTER]->(e1:Event)
With e1 as e1,count(r1) as num
with collect({event:e1,num:num}) as events

 

Match p2=(m2:Member)-[r2:BOOKMARK]->(e2:Event)
where NOT e2 IN events
with e2 as e2, count(r2) as num2, events as events
with collect({event:e2,num:num2})+collect(events) as events // order by num not sure how to sort it 
return events
 

I try apoc.coll.sort that throw errors 

Failed to invoke function `apoc.coll.sort`: Caused by: java.lang.ClassCastException: class java.util.HashMap cannot be cast to class java.lang.Comparable (java.util.HashMap and java.lang.Comparable are in module java.base of loader 'bootstrap')

 

Give this a try. It calculates the count of the registered members.  If the number is zero, then it calculates the number of bookmarked members. It then sorts the event count.

match (e:Event)
optional Match (m:Member)-[:REGISTERED]->(e:Event)
with e, count(m) as countRegistered
call {
    with countRegistered, e
    with countRegistered, e
    where countRegistered = 0
    optional match (n:Member)-[:BOOKMARKED]->(e)
    return count(n) as countBookmarked
}
return e, countRegistered + countBookmarked as eventCount
order by eventCount desc