py2neo仅获得特定标签的相关节点

时间:2019-09-19 14:00:22

标签: neo4j py2neo

因此,我们有两个标签EmployeeEmailAddress,每个EmailAddress节点最多可以通过LINKED_TO关系与其连接一个Employee节点。

下面是我的py2neo模型

class BaseNeoModel(GraphObject):
    """
    Implements some basic functions to guarantee some standard functionality
    across all models. The main purpose here is also to compensate for some
    missing basic features that we expected from GraphObjects, and improve the
    way we interact with them.
    """

    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            if hasattr(self, key):
                setattr(self, key, value)

class EmployeeNeoModel(BaseNeoModel):
    __primarylabel__ = 'Employee'
    __primarykey__ = 'IdObject'
    IdObject = Property()
    Name = Property()
    NameFamily = Property()
    NameFull = Property()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def as_dict(self):
        return {
            'user_name': self.IdObject,
            'first_name': self.Name,
            'last_name': self.NameFamily,
            'full_name': self.NameFull
        }

    def fetch(self, tribes_client_id):
        person = self.match(graph).where(IdObject=self.IdObject).first()

        if person is None:
            raise GraphQLError(f'"{self.IdObject}" has not been found in our employee list.')

        return person

    def fetch_from_email(self, tribes_client_id):
        email_address = EmailAddressNeoModel(IdObject=self.IdObject).fetch(tribes_client_id=tribes_client_id)
        # print(email_address.Name)
        connected_employees = email_address.related_employee 

        for connected_employee in connected_employees:
            print(connected_employee)

        # print(connected_employee)
        #
        # return connected_employee
        return connected_employees


class EmailAddressNeoModel(BaseNeoModel):
    __primarylabel__ = 'EmailAddress'
    __primarykey__ = 'IdObject'
    IdObject = Property()
    Name = Property()
    NameFamily = Property()
    NameFull = Property()

    related_employee = Related(EmployeeNeoModel, "LINKED_TO")

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def fetch(self, tribes_client_id):
        email_address = self.match(graph).where(IdObject=self.IdObject).first()
        return email_address

EmployeeNeoModel(IdObject="vinit@tribes.ai").fetch_from_email("demo_com")

因此,基本上上述代码中的工作是使员工节点连接到传递的EmailAddress节点。

现在,当我呼叫fetch_from_email时,我希望仅获得与传递的EmailAddress节点连接的Employee个节点,但是我得到的是所有与EmailAddress节点连接的节点,即没有Employee标签。

我通过说明related_employee = Related(EmployeeNeoModel, "LINKED_TO")的理解是告诉py2neo只返回带有Employee标签的节点,如果是的话,我是否会误解是实现上述目的的正确方法?

0 个答案:

没有答案