複数のテキストがドキュメントにリンクされているドキュメントとテキストの関係を持つ次の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")
特定のドキュメントにリンクされたすべての Text オブジェクトを取得するために、次のようにトラバーサルを使用しています。
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 に基づいてフィルタリングするより良い方法はありますか?