Fix BI Q10 by using pattern comprehensions
// Q10. Central Person for a Tag
/*
:param {
- tag: 'Che_Guevara',
- date: 20110721220000000
+ tag: 'John_Rhys-Davies',
+ date: 20120122000000000
}
*/
MATCH (tag:Tag {name: $tag})
// score
OPTIONAL MATCH (tag)<-[interest:HAS_INTEREST]-(person:Person)
+WITH tag, collect(person) AS interestedPersons
OPTIONAL MATCH (tag)<-[:HAS_TAG]-(message:Message)-[:HAS_CREATOR]->(person:Person)
WHERE message.creationDate > $date
- AND person IS NOT NULL
+WITH tag, interestedPersons + collect(person) AS persons
+UNWIND persons AS person
+// poor man's disjunct union (should be changed to UNION + post-union processing in the future)
+WITH DISTINCT tag, person
WITH
tag,
person,
- count(interest)*100 + count(message) AS score
-// friendsScore
-MATCH (person)-[:KNOWS]-(friend:Person)
-OPTIONAL MATCH (tag)<-[interest:HAS_INTEREST]-(friend)
-OPTIONAL MATCH (tag)<-[:HAS_TAG]-(message:Message)-[:HAS_CREATOR]->(friend)
-WHERE message.creationDate > $date
+ 100 * length([(tag)<-[interest:HAS_INTEREST]-(person) | interest])
+ + length([(tag)<-[:HAS_TAG]-(message:Message)-[:HAS_CREATOR]->(person) WHERE message.creationDate > $date | message])
+ AS score
+MATCH (person)-[:KNOWS]-(friend)
+WITH
+ person,
+ score,
+ 100 * length([(tag)<-[interest:HAS_INTEREST]-(friend) | interest])
+ + length([(tag)<-[:HAS_TAG]-(message:Message)-[:HAS_CREATOR]->(friend) WHERE message.creationDate > $date | message])
+ AS friendScore
RETURN
person.id,
score,
- count(interest)*100 + count(message) AS friendsScore
+ sum(friendScore) AS friendsScore
ORDER BY
score + friendsScore DESC,
person.id ASC
