Neo4j: Match multiple labels (2 or more)

后端 未结 6 1094
轮回少年
轮回少年 2020-12-07 22:14

I would like to do a search, and I would like to start traversing from 2 labels (OR condition). For example, I need to find out all the nodes which have labels either \'Male

6条回答
  •  一整个雨季
    2020-12-07 23:01

    MATCH n WHERE n:Label1 OR n:Label2
    

    ... will result in an AllNodesScan this is a bad Idea!

    maybe a better solution:

    OPTIONAL MATCH (n1:Label1)
    WITH collect(distinct n1) as c1
    
    OPTIONAL MATCH (n2:Label2) 
    WITH collect(distinct n2) + c1 as c2
    
    OPTIONAL MATCH (n3:Label3) 
    WITH collect(distinct n3) + c2 as c3
    
    UNWIND c3 as nodes
    RETURN count(nodes),labels(nodes) 
    

提交回复
热议问题