简体   繁体   中英

Neo4j Cypher — limit for “in” clause in query

I am working on a cypher query fetching nodes which are present in the path and so we are providing start node and list of nodes which are expected in the path and expected result is the nodes from the provided list.

Can you please suggest what is the limit of in clause in Neo4j like we have limit of 2100 in sql.

Example query :

MATCH (n:person{key:2529962, ownBy:0}) 
MATCH path = n<-[:relation]-(c:Equipment) 
WHERE c.key in [1505697,2406945,2408297,2408531,2410815,2413566,2415224,]
RETURN distinct EXTRACT (p in NODES(path)| p.key);

Thanks Navneet

I'm not sure what the limits are for collections, but I can easily create 100,000 with return range (1,100000) as largeCollection .

However, a better way to get those nodes is to unwind the collection into rows, and then match on the nodes with those keys:

// better to parameterize this, when you get the chance
WITH [1505697,2406945,2408297,2408531,2410815,2413566,2415224] as equipKeys
MATCH (n:person{key:2529962, ownBy:0})
UNWIND equipKeys as equipKey
MATCH (c:Equipment{key:equipKey}) 
MATCH path = (n)<-[:relation]-(c) 
RETURN distinct EXTRACT (p in NODES(path)| p.key);

EDIT

From your comment, it seems you want to only return equipment keys where the :Equipment node has a path to your :person node.

The MATCH you're currently using will find ALL possible paths, which will choke on medium to large sized graphs, especially with many relationships.

You'll likely want to make use of the EXISTS() function, which will return true or false if such a path exists. That said, you may want to consider limiting the path by giving an upper bound of possible.

WITH [1505697,2406945,2408297,2408531,2410815,2413566,2415224] as equipKeys
MATCH (n:person{key:2529962, ownBy:0}) 
UNWIND equipKeys as equipKey 
MATCH (c:Equipment{key:equipKey}) 
WITH DISTINCT n, c
WHERE EXISTS( (n)<-[:relation*0..10]-(c) )
RETURN c.key

You can easy test it:

UNWIND RANGE(0,6) as p
WITH toInt(10^p) as maxx
WITH maxx, RANGE(1,maxx) as testArray
RETURN maxx, size(testArray), maxx IN testArray, (maxx+1) IN testArray

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM