Neo4j match path exclude node with certain label

戏子无情 提交于 2019-12-07 04:06:03

问题


I am having a issue to retrieve path in neo4j exclude certain label.

Foe example, I have

               -->(h)-->(j)
              /
(a)-->(b)-->(c)-->(d)-->(i)
        \           
         -->(f)-->(g)

with h node has a Deleted label.

I have query

MATCH path = (n)-[*]->(child) where id(n)={id of node a} and NOT child:Deleted RETURN path

then I want this query to return the full path but exclude the subtree of node h since node h is Deleted.

the return tree should be like

(a)-->(b)-->(c)-->(d)-->(i)
        \           
         -->(f)-->(g)

But the query seems not working.

Can any one help me with this.

Thanks


回答1:


What worked for me is a list comprehension over nodes in the path:

MATCH path = ()-[*]->()
WHERE NONE(n IN nodes(path) WHERE n:Deleted)
RETURN path



回答2:


You can use APOC Procedures with this, using the path expander functionality along with a blacklist to prevent expanding to nodes with labels in the blacklist during traversal.

match (a)
where id(a) = {id of node a}
call apoc.path.expandConfig(a, {labelFilter:'-Deleted'}) yield path
return path



回答3:


You need to check both nodes for the label:

match p=((a)-[r:NEXT]->(b))
where  not (a:Deleted or b:Deleted)
return p

here's another example:

match (a)-[r:NEXT]->(b) 
where  not (("Deleted" in labels(a)) or
            ("Deleted" in labels(b))) 
return a, r, b


来源:https://stackoverflow.com/questions/36439749/neo4j-match-path-exclude-node-with-certain-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!