Skip to content

Commit e64df5c

Browse files
committed
Added example about parameterizing between from earlier values
This closes #226
1 parent b109241 commit e64df5c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

book/Section-Writing-Gremlin-Queries.adoc

+33
Original file line numberDiff line numberDiff line change
@@ -4960,6 +4960,39 @@ g.V().has('airport','city','London').
49604960
[country:[CA],code:[YXU],region:[CA-ON]]
49614961
----
49624962

4963+
We've seen how the number of 'by' modulators correspond to the parameters in the
4964+
'where' and that they are applied in a round-robin fashion, starting with the
4965+
value of the traverser being compared and then to the values it is to be compared
4966+
with. We can further see this on display when we look at a query that uses 'between'
4967+
where there are three arguments to consider. For example, let's imagine we wanted to
4968+
find five airports that have a number of runways that are between one less and one
4969+
more than 'IAD'.
4970+
4971+
[source,groovy]
4972+
----
4973+
g.V().has('airport','code','IAD').values('runways').as('iadl','iadh').
4974+
V().hasLabel('airport').
4975+
where(between('iadl','iadh')).
4976+
by('runways').
4977+
by(math('_ - 1')).
4978+
by(math('_ + 1')).
4979+
limit(5).
4980+
values('code').fold()
4981+
4982+
[ANC,BNA,BWI,DCA,KNS]
4983+
----
4984+
4985+
In this prior example, we find IAD and then keep that value in two step labels. Those
4986+
labels are used later as arguments to 'between'. Note that there are three 'by'
4987+
modulators that follow the 'where'. The first refers to the current airport vertex
4988+
that we are considering for this filter and that 'by' ensures we use the runways
4989+
value in the comparison to 'between'. You can see that this initial 'by' relates to
4990+
an implied reference to the current traverser as there is no label for it present in
4991+
the 'where'. The second two 'by' modulators refers to the step labels in the
4992+
'between' and do a 'math' operation that subtract and add 1 to the number of runways
4993+
for 'IAD'. In this way, you get the effect of dynamically calculating filtering
4994+
arguments based on a prior portion of a query.
4995+
49634996
Let's imagine that we want to write a query to find all airports that have the same
49644997
region code as the French airport of Nice. Let's assume for now that we do not know
49654998
what the region code is so we cannot just write a simple query to find all airports

0 commit comments

Comments
 (0)