@@ -1682,9 +1682,105 @@ g.V().has('region','US-TX').fold().skip(local,3)
1682
1682
There are many other ways to specify a range of values using Gremlin. You will find
1683
1683
several additional examples in the "<<tranges>>" section.
1684
1684
1685
+ Working with the end of a stream
1686
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1687
+
1688
+ When we want to take items from the end of the stream, we typicaly use the 'tail'
1689
+ step. With 'tail' you specify the number of elements to take from the end of the
1690
+ stream.
1691
+
1692
+ [source,groovy]
1693
+ ----
1694
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1695
+ order().by('code').
1696
+ tail(1).
1697
+ values('code').fold()
1698
+
1699
+ [JFK]
1700
+
1701
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1702
+ order().by('code').
1703
+ tail(2).
1704
+ values('code').fold()
1705
+
1706
+ [IAD,JFK]
1707
+ ----
1708
+
1709
+ In the prior examples, we use 'order' to ensure consistent results for purpose of
1710
+ demonstration and you often must use 'order' in your own query writing to achieve the
1711
+ same, but note that if you are using 'order' , it might be better to rewrite the first
1712
+ as follows:
1713
+
1714
+ [source,groovy]
1715
+ ----
1716
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1717
+ order().by('code',desc).
1718
+ limit(1).
1719
+ values('code').fold()
1720
+
1721
+ [JFK]
1722
+ ----
1723
+
1724
+ By using a 'desc' order, we can move JFK to the front of our stream and simply take
1725
+ the first item it encounters, which likely speeds up the query considerably as most
1726
+ graph databases should optimize the 'order' and we likely spare Gremlin from having
1727
+ to iterate the entire stream to get to the last item with 'tail' . Depending upon your
1728
+ requirements, you might find that you could use the same tactic for the second query
1729
+ but note that the results have a slight difference.
1730
+
1731
+ [source,groovy]
1732
+ ----
1733
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1734
+ order().by('code',desc).
1735
+ limit(2).
1736
+ values('code').fold()
1737
+
1738
+ [JFK,IAD]
1739
+ ----
1740
+
1741
+ In the prior example you can see that we get the same two results, but they are in
1742
+ reversed order. You would have to reverse the 'order' to get the same result as the
1743
+ first query. In these examples, we are dealing with a small dataset with a small set
1744
+ of five results so the performance implications are not big for either approach, but
1745
+ for larger scales you may find yourself making choices with these patterns that can
1746
+ have significant impact.
1747
+
1748
+ Sometimes you may find that you want to take the second to last item in the stream.
1749
+ Finding the penultimate element of the stream just means taking the first item found
1750
+ in the list of the last two:
1751
+
1752
+ [source,groovy]
1753
+ ----
1754
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1755
+ order().by('code').
1756
+ tail(2).limit(1).
1757
+ values('code').fold()
1758
+
1759
+ [IAD]
1760
+ ----
1761
+
1762
+ Gremlin does not have a step for getting all elements of the stream except for the
1763
+ last one. The following pattern allows you to do this by using 'store' to hold the
1764
+ 'tail' of the stream, which you can then use later to filter it away.
1765
+
1766
+ [source,groovy]
1767
+ ----
1768
+ g.V().has('airport','code', within('IAD','DAL','JFK','DCA','DFW')).
1769
+ order().by('code').
1770
+ fold().
1771
+ sideEffect(tail(local).store('t')).unfold().where(neq('t')).by().by(unfold()).
1772
+ values('code').fold()
1773
+
1774
+ [DAL,DCA,DFW,IAD]
1775
+ ----
1776
+
1777
+ The pattern demonstrated in the prior examples shows how flexible Gremlin can be.
1778
+ With a solid command of the steps Gremlin offers and a clever application of them you
1779
+ can accomplish a great many things.
1780
+
1685
1781
[[dedup]]
1686
1782
Removing duplicates - introducing 'dedup'
1687
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1783
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1688
1784
1689
1785
It is often desirable to remove duplicate values from query results. The 'dedup' step
1690
1786
allows us to do this. If you are already familiar with Groovy collections, the
0 commit comments