【问题标题】:Find Relation 1 to n using cypher query使用密码查询查找关系 1 到 n
【发布时间】:2017-03-15 09:45:58
【问题描述】:

entity1 -> entity2 -> entity3 -> entity4 -> entity5

起始实体是 entity1 我想得到其中一个响应,直到从起始实体到连接实体的关系。

可能是:-

 1) entity1
 2) entity1 -> entity2
 3) entity1 -> entity2 -> entity3
 4) entity1 -> entity2 -> entity3 -> entity4
 5) entity1 -> entity2 -> entity3 -> entity4 -> entity5

我试过了, 全部从 entity3 连接。

  MATCH (n1:entity1)-[:Relationentitys]->(n2:entity2)-
  [:Relationentitys]->(n3:entity3)-[:Relationentitys]->(n4:entity4)-
  [:Relationentitys]->(n5:entity5) where n3.id='5084712c801f' AND n3.tag='entity3' RETURN n1,n2,n3,n4,n5

关系实体在层次结构中相互连接。

我不知道如何使用密码。

【问题讨论】:

  • 这是否总是由最多 5 个实体组成的链,而不是一棵树,关系分支到您想要的实体之外的其他节点?
  • 是的,它是一个实体链。

标签: neo4j cypher


【解决方案1】:

如果节点总是形成这些简单的链,从 1 到 5 个实体,没有分支到其他节点,那么您可能可以使用简单的变长关系匹配来获取链中的所有实体节点:

// from the given entity, track back to entity1, the start of the chain
MATCH (n3:entity3)<-[:Relationentitys*0..4]-(en1:entity1)
WHERE n3.id='5084712c801f' AND n3.tag='entity3' 
// now get the full chain from entity1
MATCH (en1)-[:Relationentitys*0..4]->(en)
RETURN COLLECT(en) as entities

这应该为您提供链中实体的集合,最多可存在于链中。

【讨论】:

  • 感谢您的回复。
  • 当然。我仍然不完全清楚您的需求。无论您匹配哪个实体,上述内容都应该有效。如果顺序不那么重要,有办法改善这一点。
  • 是的,我想了解更多,根据我的回复我还是无法得到。
  • 那么请在此处更新您的问题,详细说明您想要什么,并提供一些关于为什么这不适合您的详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
相关资源
最近更新 更多