简体   繁体   English

在具有不同属性的节点之间创建相同的关系

[英]Create same relationships between nodes with different properties

I'm creating a simple social graph where a user can create a post, tag it, and comment on it.我正在创建一个简单的社交图,用户可以在其中创建帖子、标记它并对其发表评论。 I'm using py2neo to do the modelling.我正在使用py2neo进行建模。 The model has user and post as nodes.该模型将userpost作为节点。 A user TAGGED , POSTED , or COMMENTED on a post .用户对TAGGED进行了POSTEDpostCOMMENTED In my case, a single user can create multiple tag s or comment s on a single post (just like any social network out there).在我的例子中,单个用户可以在单个post上创建多个tagcomment (就像那里的任何社交网络一样)。 Based on my model, this necessitates multiple TAGGED or COMMENTED relationships but with distinct properties .根据我的模型,这需要多个TAGGEDCOMMENTED关系但具有不同的属性 The model is built thusly:该模型是这样构建的:

from py2neo.ogm import (
    GraphObject, 
    Property, 
    RelatedTo, 
    RelatedFrom
)


class User(GraphObject):
    __primarykey__ = 'name'

    name = Property()

    posts = RelatedTo('Post', 'POSTED')
    comments = RelatedTo('Post', 'COMMENTED')
    tags = RelatedTo('Post', 'TAGGED')

    def __init__(self, name):
        self.name = name


class Post(GraphObject):

    # assumes __id__ as primary key because
    # left undefined

    title = Property()

    users = RelatedFrom('User', 'POSTED')
    comments = RelatedFrom('User', 'COMMENTED')
    tags = RelatedFrom('User', 'TAGGED')

    def __init__(self, title):
        self.title = title

I run the following to build the graph:我运行以下命令来构建图形:

user = User(name='john')
post = Post(title='Wow!')

user.posts.add(
    post,
    {'date': '2017-04-26'}
)
graph.push(user)

user.comments.add(
    post,
    {'caption': 'I know!', 'date': '2017-04-26'}
)
graph.push(user)

for tag in ['yellow', 'green']:
    user.tags.add(
        post,
        {'tag': tag, 'date': '2017-04-26'}
    )
    graph.push(user)

I would expect there to be two TAGGED relationships, something like this:我希望有两个TAGGED关系,如下所示:

期待看到

But I see this is not the case:但我看到情况并非如此:

在此处输入图像描述

My question then is twofold.那么我的问题是双重的。 (1) Can create a multiple relationships of the same type with different properties? (1) 能否创建多个同类型不同属性的关系? (2) Is this the best model choice for the use case? (2) 这是用例的最佳模型选择吗?

You can use neo4jrestclient.你可以使用 neo4jrestclient。 It alows you to have multiple relationships of the same type and it's quite easy to use too.它允许您拥有多个相同类型的关系,而且它也很容易使用。 You can use the following code:您可以使用以下代码:

    from neo4jrestclient.client import GraphDatabase
    gdb=GraphDatabase("http://localhost:7474/db/data/")
    user=gdb.nodes.create(name='john')
    post=gdb.nodes.create(title='wow')
    user.labels.add('User')
    post.labels.add('Post')
    u=gdb.labels.get('User')
    p=gdb.labels.get('Post')

now for multiple relationships现在为多重关系

    for tag in ['yellow', 'green']:
        u.get(name='john')[0].relationships.create('TAGGED',p.get(id=0)[0],tag=tag,date='2017-04-26')

this should do it.这应该做到。 The.get is used to update a node much like.push. .get 用于更新节点很像.push。 And there can be much cleaner way to do this, but you get the idea.可以有更简洁的方法来做到这一点,但你明白了。 The documentation is pretty decent too.文档也很不错。 https://readthedocs.org/projects/neo4j-rest-client/downloads/pdf/latest/ https://readthedocs.org/projects/neo4j-rest-client/downloads/pdf/latest/

Although neo4j (and most programming interfaces to neo4j, like Cypher) does support multiple relationships of the same type (with possibly differing property sets) between a single pair of nodes, py2neo does not seem to (see this issue ).尽管 neo4j(以及大多数 neo4j 的编程接口,如 Cypher)确实支持一对节点之间相同类型的多个关系(可能具有不同的属性集), py2neo似乎不支持(请参阅本期)。

I suggest that you consider using some other way to use neo4j from Python , like the officially supported neo4j Python Driver .我建议您考虑使用其他方式从 Python 使用 neo4j ,例如官方支持的neo4j Python 驱动程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM