Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions docs/src/reference/the-traversal.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7386,20 +7386,46 @@ g.V('42a')
----

Other `Graph` implementations generate element identifiers automatically and cannot be assigned.
As a helper, `ElementIdStrategy` can be used to make identifier assignment possible by using vertex and edge indices
under the hood. Note that this demonstration is using `TinkerGraph` for convenience, however in practice
`ElementIdStrategy` offers little value in a `Graph` which allows custom identifiers.
As a helper, `ElementIdStrategy` makes identifier assignment possible by storing the assigned identifier in a
property, which is `__id` by default, and by translating identifier-based operations onto that property. Lookups such
as `g.V(id)`, `g.E(id)` and `hasId()` become filters on the property, `id()` returns its value, and an identifier
given to `addV()` or `addE()` with `property(id, value)` is written to it. When no identifier is supplied, the strategy
generates one, which is a random UUID string by default. Note that this demonstration is using `TinkerGraph` for
convenience, however in practice `ElementIdStrategy` offers little value in a `Graph` which allows custom identifiers.

[gremlin-groovy]
----
strategy = ElementIdStrategy.build().create()
g = traversal().withEmbedded(TinkerGraph.open()).withStrategies(strategy)
g.addV().property(id, '42a').id()
g.addV().property(id, '43b').id()
g.addE('knows').from(__.V('42a')).to(__.V('43b')).property(id, 'e1').id()
g.E('e1').id()
g.V('42a').out('knows').id()
g.V().elementMap()
----

IMPORTANT: The key that is used to store the assigned identifier should be indexed in the underlying graph
database. If it is not indexed, then lookups for the elements that use these identifiers will perform a linear scan.
The strategy rewrites only the traversal steps that refer to the identifier, so each underlying element keeps the
identifier that the graph generated for it. Output produced from the element itself, such as `elementMap()` or the
string form of a returned element, shows that graph-generated identifier and lists the assigned identifier as the
`__id` property, while `id()` returns the assigned identifier.

The property key and the identifier generator are set on the builder with `idPropertyKey()` and `idMaker()`
respectively. See the
link:https://tinkerpop.apache.org/javadocs/x.y.z/core/org/apache/tinkerpop/gremlin/process/traversal/strategy/decoration/ElementIdStrategy.Builder.html[ElementIdStrategy.Builder javadoc]
for more information on these settings.

[gremlin-groovy]
----
strategy = ElementIdStrategy.build().idPropertyKey('customId').create()
g = traversal().withEmbedded(TinkerGraph.open()).withStrategies(strategy)
g.addV().property(id, '42a').id()
g.V('42a').elementMap()
----

IMPORTANT: The property key that stores the assigned identifier, `__id` unless changed with `idPropertyKey()`, should
be indexed in the underlying graph database. If it is not indexed, then lookups for the elements that use these
identifiers will perform a linear scan.

=== EventStrategy

Expand Down
Loading