图形数据库Newbie Q-如何决定2个节点之间的关系的方向 [英] Graph Database Newbie Q- How to decide on the direction of a relation between 2 nodes

查看:261
本文介绍了图形数据库Newbie Q-如何决定2个节点之间的关系的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何决定关系的动词方向?



例如,我有一个国家/地区属于Sub REgion,属于Sub区域。



(区域) - [HAS] - >(子区域) - [ HAS] - >(国家/地区)





(地区)< - [BELONGS_TO] Region)< - [BELONGS_TO] - (国家/地区)



尊重
San

方案

我同意@InverFalcon的方向性主要是一个主观的决定。但是,可能有(至少)一种情况,你可能想使用一个特定的方向,特别是如果这将使一个重要的用例更快。



这是相关的事实,如果你可以使一个Cypher模式较少具体(不影响输出),那么neo4j将不得不做更少的工作,你的查询会更快。



例如,假设整个数据模型包含2个节点标签和2个关系类型,如下所示。 (我使用我自己的数据模型,因为我不知道你的用例是什么。)

  [:ACTED_IN]  - >(:电影)
(:人) - [:DIRECTED] - >(:电影)

为了找到一个演员行动的电影,你的查询将看起来像下面。 (注意,我们必须指定 ACTED_IN 类型,因为外向关系也可以是 DIRECTED neo4j必须明确地测试它的类型的每个传出关系):

  MATCH(:Person {id:123} :ACTED_IN]  - >(m:Movie)
返回m;但是,如果你的数据模型替换了 DIRECTED ,那么


$ b < >类型与具有相反方向性的 DIRECTED_BY 类型,则它将看起来像这样:

 (:Person) -  [:ACTED_IN]  - >(:Movie)
(:Person)< - [:DIRECTED_BY] - (:Movie)



有了这个调整,你的查询可以更简单和更快速(因为neo4j不必测试关系类型):

  MATCH(:Person {id:123}) - >(m:Movie)
RETURN m;

要完成,请注意,在上述2 MATCH :Movie 标签,因为在这两个数据模型中, ACTED_IN 结束节点将始终具有 Movie 标签。


How do you decide the verb-direction of a relation ?

E.g I have a Country falling under a Sub REgion which in turn is under a Region. Which one would be better and are there any thumb rules on deciding the direction.

(Region)-[HAS]->(Sub Region)-[HAS]->(Country)

or

(Region)<-[BELONGS_TO]-(Sub Region)<-[BELONGS_TO]-(Country)

Regards San

解决方案

I agree with @InverFalcon that directionality is mostly a subjective decision. However, there may be (at least) one situation in which you might want to use a specific direction, especially if that will make an important use case faster.

This is related to the fact that often if you can make a Cypher pattern less specific (without affecting the output), then neo4j would have to do less work and your query would be faster.

For example, suppose your entire data model consists of 2 node labels and 2 relationship types, like below. (I use my own data model, since I don't know what your uses cases are.)

(:Person)-[:ACTED_IN]->(:Movie)
(:Person)-[:DIRECTED]->(:Movie)

In order to find the movies that an actor acted in, your query would have to look something like the following. (Notice that we have to specify the ACTED_IN type, because an outgoing relationship could also be of type DIRECTED. This means that neo4j has to explicitly test every outgoing relationship for its type):

MATCH (:Person {id: 123})-[:ACTED_IN]->(m:Movie)
RETURN m;

However, if your data model replaced the DIRECTED type with a DIRECTED_BY type that had opposite directionality, then it would look like this instead:

(:Person)-[:ACTED_IN]->(:Movie)
(:Person)<-[:DIRECTED_BY]-(:Movie)

With that tweak, your query could be simpler and faster (since neo4j would not have to test relationship types):

MATCH (:Person {id: 123})-->(m:Movie)
RETURN m;

And to be complete, notice that in the above 2 MATCH patterns we could actually remove the :Movie label, since in both data models the ACTED_IN end node will always have the Movie label.

这篇关于图形数据库Newbie Q-如何决定2个节点之间的关系的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆