Neo4J cypher query to get nodes connected by paths with the same generic attribute

情到浓时终转凉″ 提交于 2019-12-11 05:45:54

问题


I'm trying to create a cypher query that returns me the nodes connected by a given range of hops (i.e. 1..5), where all the relationships between these hops share a same attribute value, without specifying this attribute.

So I would like to do something like

MATCH (a {type: 'cin1'})-[rels:Next*1.. {value: 1}]->(b {type: 'cancer'}) 
RETURN (a), (b)

But without specifying that the value on the edges should be one, they just need to be equal among all the edges in the hopping process.


回答1:


I would add an upper bound to your path. Or use (all)shortestPath(s)

Also make sure to look up a and b by indexed label + property combination.

And then you can use a predicate on the relationships-collection that forms your path.

MATCH (a:Label {type: 'cin1'})
MATCH (b:Label {type: 'cancer'})
MATCH shortestPath((a)-[rels:Next*1..20]->(b))
WHERE ALL(r in tail(rels) WHERE (head(rels)).value = r.value)
RETURN (a), (b)


来源:https://stackoverflow.com/questions/38095528/neo4j-cypher-query-to-get-nodes-connected-by-paths-with-the-same-generic-attribu

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