Query if else in gremlin
I have a query and that return a list vertex. I want to do a query from those vertexes like this (if - else):
- vertex doesn't has out edge -> return itself
- vertex has out edge -> then keep querying until there are no edges out ( repeat(out()).until(outE().count().is(0)) )
I'm trying to limit the use of loops here to improve performance. Who can help me.
Thank you very much
7 Replies
The functionality you are describing is basically what
repeat()
does by default, but you have to tell it to actually "return itself" with emit()
. Here's an example:
The above traverses out()
from v[1]
until it runs out of edges to follow. If you add emit()
(which takes a predicate and defaults to true
when none is supplied) you tell repeat()
under what circumstance that you want to actually put those vertices into the stream, so:
In your case, you really don't want all of those vertices because v[4]
doesn't match your criteria as it has outgoing edges. You can prevent it from being emitted like:
There are some duplicates in the case of the "modern" graph, you could add dedup()
to the end to polish that up:
i think you should ask a separate question for that. that way janusgraph folks might have a better chance of spotting it. i don't know the answer offhand.
ok, thank you
In your query .
If V(1) doesn't have out edge, it will not return V(1). While i want return itself if it doesn't have out edge
you could use
optional()
in this case:
oh, thank you