【问题标题】:How to filter neomodel nodes by their attributes after traversal?遍历后如何按属性过滤neomodel节点?
【发布时间】:2020-05-02 16:02:46
【问题描述】:

我有以下具有文档和文本关系的 neo4j 模型,其中多个文本链接到一个文档:

class Document(StructuredNode):
    """Document Neo4J model for documents"""
    document_id = IntegerProperty(required = True)


class Text(StructuredNode):
    """Text model for managing texts"""

    # Inherent text properties
    text_id = IntegerProperty(required = True)
    text = StringProperty(required = True)

    document = RelationshipTo('Document', "PARENT")

为了获取与特定文档链接的所有文本对象,我使用如下遍历:

def get_all_texts_by_document_id(document_id):
    document = Document.nodes.get_or_none(document_id = document_id)
    definition = dict(node_class = Text, direction = EITHER, relation_type = "PARENT", model = None)
    document_to_text_traversal = Traversal(document, Text.__label__, definition)
    texts = document_to_text_traversal.all()
    print(f"Returning {len(texts)} texts for document_id {document_id}")
    return texts

我想获取具有特定文本 ID 的文本,目前我正在使用 if 条件对使用上述函数接收到的所有文本列表进行迭代。

遍历完成后,是否有基于 text_id 进行过滤的内置方法/更好的方法?

【问题讨论】:

    标签: python database neo4j neomodel


    【解决方案1】:

    我不知道在用 neomodel 遍历后有任何过滤节点的方法。

    但是,如果您不必使用 Traversal 对象,则可以使用以下内容:

    • 通过将此行添加到您的 Document 类来添加从文本到文档的关系:

      texts = RelationshipFrom('Text', "PARENT")
      
    • 按照 PARENT 关系对端节点进行过滤:

      document = Document.nodes.get_or_none(document_id=1)
      if document:
          texts = document.texts.filter(text_id=1)
          print(texts.all())
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      相关资源
      最近更新 更多