neo4j/community — Issue #207: Cypher query aggregated functions (COUNT etc.) return Integer, should be Long
Author: ceefour
Created: 2012-01-29 05:16:36+00:00
State: open

Example code: (Scala)

val logger = LoggerFactory.getLogger(classOf[GraphAnalysis])

@Inject var userRepo: UserRepository = _
@Inject var neo4j: Neo4jTemplate = _

/**
* Exclude if already following.
*/
@CacheResult
def getMutualLikesLookupExclusive(userId: Long): java.util.List[MutualLikeRow] = {
logger.debug("getMutualLikesLookupExclusive {}", userId)

val user = userRepo.findOne(userId)
val followingIds = user.getFollowUsers.map(_.getNodeId)

val rows = neo4j.query(
"START x=node("+ userId +") MATCH x-[:LIKE]->i<-[:LIKE]-y RETURN id(y) AS id, y.name AS name, COUNT(*) AS mutualLikeCount LIMIT 100", null)
val nonfriendResult = rows.filter( it => !followingIds.contains(it("id")) )
val rawResult = nonfriendResult.map { it =>
new MutualLikeRow(it("id").asInstanceOf[Long], it("mutualLikeCount").asInstanceOf[Int],
it("name").asInstanceOf[String]) }.toList
val result = rawResult.sortBy { 0 - _.mutualLikeCount }
logger.debug("getMutualLikesLookupExclusive {} = {}", userId, result)
result
}

The query returns `id` (`nodeId`) as `Long`, however the `COUNT` is returned as `Integer`.
Considering that nodes can grow beyond Integer range, any calculation, especially involving aggregates, should return the type as `Long`.

Originally filed as https://jira.springsource.org/browse/DATAGRAPH-183