1

我有以下具有 Document 和 Text 关系的 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 过滤的内置方法/更好的方法?

4

1 回答 1

2

我不知道用新模型遍历后有任何过滤节点的方法。

但是,如果您不必使用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())
    
于 2020-05-18T18:37:35.943 回答