Type casting between storable and virtual

Run this to get a list of all internal Ids for all nodes with label itemA:
match(a:itemA) with collect(id(a)) as IdList merge (c:cache) set c.IdList = IdList

Then run this to try and retrieve all nodes with label itemA, whose internal Id is in the list you previously stored:
match (c:cache) with c.IdList as IdList match (a:itemA) where id(a) in IdList return count(a)

Observe Neo explode because it can't convert org.neo4j.values.storable.LongArray (the array you stored in the node with label cache) to org.neo4j.values.virtual.ListValue (the list in memory). Both of these lists were generated using the Id() function. Any ideas how to resolve this error? Do we need to run a cast somewhere?

Hello @sporritt and welcome to the Neo4j community :slight_smile:

Yes, you can use the toInteger() function:

MATCH (c:cache)
WITH c.IdList AS IdList
MATCH (a:itemA)
WHERE toInteger(id(a)) IN IdList
RETURN count(a)

Regards,
Cobra

Thanks @Cobra . Although this does get around the type casting issue, it performs even worse than the previous solution we were using. For reference, we're currently using UNWIND on the stored list then COLLECT on the result, but the UNWIND operation is pretty slow (circa 30 seconds over 0.5million nodes). With the toInteger solution you suggested, 50 nodes takes about 1 second and 0.5million nodes takes hours.

One thing you can try is to use apoc.coll.partition to split IdList into small chunks like,
apoc.coll.partition(IdList, 2500) and save each partition as a separate property of (c:cache) node.