Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gremlin query: Another way to write this in a loop

g.v(1).out('__SYSTEM_HAS_CHILD').filter{it.name == 'Journal'}.out('__SYSTEM_HAS_CHILD').filter{it.name == 'travel'}.out('__SYSTEM_HAS_CHILD').filter{it.name == 'Alaskan-Natives'}.map

Maybe store an array with items, then loop over each one performing an out and attaching it to it.name; and iterating (counting) to ensure we don't proceed over the length of the array.

like image 810
kr1zmo Avatar asked Jan 01 '26 20:01

kr1zmo


1 Answers

You can reformat the pipeline like this:

pipe = g.v(1)
pipe = pipe.out('__SYSTEM_HAS_CHILD').filter{it.name == 'Journal'}
pipe = pipe.out('__SYSTEM_HAS_CHILD').filter{it.name == 'travel'}
pipe = pipe.out('__SYSTEM_HAS_CHILD').filter{it.name == 'Alaskan-Natives'}
pipe.map

Then you can use Groovy constructs to make that into a loop:

names = ["Journal", "travel", "Alaskan-Natives"]
pipe = g.v(1)
names.each() { name -> 
  pipe = pipe.out('__SYSTEM_HAS_CHILD').filter{it.name == name} 
}
pipe.map

NOTE: Why are you returning the pipeline as a map? To iterate a pipeline, you can use either:

pipe.iterate()
pipe.toList()

See https://github.com/tinkerpop/gremlin/wiki/Gremlin-Methods

like image 115
espeed Avatar answered Jan 03 '26 12:01

espeed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!