neo4j:单向/双向关系? [英] neo4j: one-directional / two-directional relationships?

查看:578
本文介绍了neo4j:单向/双向关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我研究了neo4j,由于它的数据模型很适合我的项目,所以我可能会在即将到来的项目中使用它.我浏览了文档,但仍然需要这个问题的答案:

So I looked into neo4j, and I may be using it in an upcoming project since its data model might fit my project very well. I looked through the docs but I still need an answer to this question:

我可以将关系设置为单向吗?

看来neo4j人们喜欢看电影,所以让我们继续吧.如果我有一个这样的图:

It seems that the neo4j people like movies so let's continue with that. If I have a graph like this:

Actor A -> [:Acts in] -> Movie B

然后方向很明显,因为节点是不同的类型.

then direction is obvious, because the nodes are different types.

但是我喜欢恐怖电影,所以...

But I like horror movies so...

Person A -> [:wants_to_kill] -> Person B

我需要这种关系是单向的,因此如果我查询"A人想要杀死谁?"如果我询问"B人想要杀死谁?",我得到B人.我什么也没得到.

I need this relationship to be one-directional so if I query "Who does Person A want to kill?" I get Person B, if I query "Who does Person B want to kill?" I get nothing.

有时候我仍然需要双向关系

赞:

Person A <-[:has_met] -> Person B

...这很明显.

文档说:

Relationships are equally well traversed in either direction. This means that there is
no need to add duplicate relationships in the opposite direction (with regard to 
traversal or performance).

While relationships always have a direction, you can ignore the direction where it is 
not useful in your application.

因此文档说,默认情况下,关系具有方向,如果愿意,我可以忽略它.

So docs say, relationships by default have an direction and I can ignore that if I wish.

现在这是使事情变得复杂的地方:

Now this is where things get complicated:

考虑以下图表(并注意箭头)

Consider the following graph (and note the arrows)

Person A <- [:wants_to_kill] -> Person B
Person B -> [:wants_to_kill] -> Person C
Person C -> [:wants_to_kill] -> Person A

如果我忽略所有[:wants_to_kill]的说明,则会得到错误的结果 代表"A/C人想杀谁?" 如果我知道必须忽略哪些内容,则不会进行查询.

If I ignore the Directions for all [:wants_to_kill] I get false results for "Who does Person A / C want to kill?" If I knew which ones I had to ignore, I would not do the query.

那么我可以以某种方式将关系设置为双向的(在创建它们时),还是应该使用两个关系(在Person A和B之间)建模呢?

So can I somehow set relationships to be two-directional (when creating them), or should I model this with two relationships (between Person A & B)?

推荐答案

Neo4j中的关系始终具有方向.如果关系类型的语义不包含方向,例如has_met在您的示例中,那么最佳做法是在创建关系时应用任意方向.然后使用cypher中的双向"(没有大于/小于"字符)符号进行查询:

A relationship in Neo4j always has a direction. If a relationship type's semantics do not incorporate a direction, e.g. has_met from your example, then it's best practice to apply an arbitrary direction on creation of the relationship. Querying is then done by using the "both directions" (there is no "larger/smaller than" character) notation in cypher:

start ... match (a)-[:HAS_MET]-(b) ....

相反,如果关系的语义确实具有与wants_to_kill相似的方向,则需要使用两个关系来表示a和b想要杀死另一个,反之亦然.对于上面的示例,您需要具有4个关系:

In contrast, if the semantics of a relationship do have a direction like your wants_to_kill, you need to use two relationship to indicate that a and b wants kill the other one and vice versa. For the example above, you need to have 4 relationships:

Person A -[:wants_to_kill]-> Person B
Person B -[:wants_to_kill]-> Person A
Person B -[:wants_to_kill]-> Person C
Person C -[:wants_to_kill]-> Person A

要找到所有想要杀死A的人,您会做的事:

To find all the person that A wants to kill you do:

start a=node:node_auto_index(name='A') match a-[:wants_to_kill]->victims_of_a return victims_of_a

找到所有想杀死A的人:

To find all persons who want to kill A:

start a=node:node_auto_index(name='A') match murderer_of_a-[:wants_to_kill]->a return murderer_of_a

这篇关于neo4j:单向/双向关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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