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