You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -146,3 +146,15 @@ In some cases though, it is not possible to set explicit aliases, for example wh
146
146
.. note::
147
147
148
148
When using the resolvers in combination with a traversal as in the example above, it will resolve the variable name of the last element in the traversal - the Species node for NodeNameResolver, and Coffee--Species relationship for RelationshipNameResolver.
149
+
150
+
Another example is to reference the root node itself::
Copy file name to clipboardExpand all lines: doc/source/traversal.rst
+68-5Lines changed: 68 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,9 @@ Path traversal
6
6
7
7
Neo4j is about traversing the graph, which means leveraging nodes and relations between them. This section will show you how to traverse the graph using neomodel.
8
8
9
-
We will cover two methods : `traverse_relations` and `fetch_relations`. Those two methods are *mutually exclusive*, so you cannot chain them.
9
+
For this, the method to use is `traverse`.
10
+
11
+
Note that until version 6, two other methods are available, but deprecated : `traverse_relations` and `fetch_relations`. Those two methods are *mutually exclusive*, so you cannot chain them.
10
12
11
13
For the examples in this section, we will be using the following model::
12
14
@@ -27,6 +29,59 @@ For the examples in this section, we will be using the following model::
27
29
Traverse relations
28
30
------------------
29
31
32
+
The `traverse` allows you to define multiple, multi-hop traversals, optionally returning traversed elements.
33
+
34
+
For example, to find all `Coffee` nodes that have a supplier, and retrieve the country of that supplier, you can do::
35
+
36
+
Coffee.nodes.traverse("suppliers__country").all()
37
+
38
+
This will generate a Cypher MATCH clause which traverses `Coffee<--Supplier-->Country`, and by default will return all traversed nodes and relationships.
39
+
40
+
This method allows you to define a more complex `Path` object, giving you more control over the traversal.
41
+
42
+
You can specify which elements to return, like::
43
+
44
+
# Return only the traversed nodes, not the relationships
The `Country` nodes matched will be made available for the rest of the query, with the variable name `country`. Note that this aliasing is optional. See :ref:`Advanced query operations` for examples of how to use this aliasing.
60
+
61
+
.. note::
62
+
63
+
The `traverse` method can be used to traverse multiple paths, like::
This will generate a Cypher MATCH clause that traverses both paths `Coffee<--Supplier-->Country` and `Coffee<--Pub-->City`.
68
+
69
+
.. note::
70
+
71
+
When using `include_rels_in_return=True` (default), any relationship that you traverse using this method **MUST have a model defined**, even if only the default StructuredRel, like::
72
+
73
+
class Person(StructuredNode):
74
+
country = RelationshipTo(Country, 'IS_FROM', model=StructuredRel)
75
+
76
+
Otherwise, neomodel will not be able to determine which relationship model to resolve into, and will fail.
77
+
78
+
Traverse relations (deprecated)
79
+
-------------------------------
80
+
81
+
.. deprecated:: 5.5.0
82
+
83
+
This method is set to disappear in version 6, use `traverse` instead.
84
+
30
85
The `traverse_relations` method allows you to filter on the existence of more complex traversals. For example, to find all `Coffee` nodes that have a supplier, and retrieve the country of that supplier, you can do::
@@ -43,8 +98,12 @@ The `Country` nodes matched will be made available for the rest of the query, wi
43
98
44
99
This will generate a Cypher MATCH clause that enforces the existence of at least one path like `Coffee<--Supplier-->Country` and `Coffee<--Pub-->City`.
45
100
46
-
Fetch relations
47
-
---------------
101
+
Fetch relations (deprecated)
102
+
----------------------------
103
+
104
+
.. deprecated:: 5.5.0
105
+
106
+
This method is set to disappear in version 6, use `traverse` instead.
48
107
49
108
The syntax for `fetch_relations` is similar to `traverse_relations`, except that the generated Cypher will return all traversed objects (nodes and relations)::
50
109
@@ -59,8 +118,12 @@ The syntax for `fetch_relations` is similar to `traverse_relations`, except that
59
118
60
119
Otherwise, neomodel will not be able to determine which relationship model to resolve into, and will fail.
61
120
62
-
Optional match
63
-
--------------
121
+
Optional match (deprecated)
122
+
---------------------------
123
+
124
+
.. deprecated:: 5.5.50
125
+
126
+
This method is set to disappear in version 6, use `traverse` instead.
64
127
65
128
With both `traverse_relations` and `fetch_relations`, you can force the use of an ``OPTIONAL MATCH`` statement using the following syntax::
0 commit comments