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
spmallette
spmallette2y ago
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:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).repeat(out())
gremlin>
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).repeat(out())
gremlin>
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:
gremlin> g.V(1).repeat(out()).emit()
==>v[3]
==>v[2]
==>v[4]
==>v[5]
==>v[3]
gremlin> g.V(1).repeat(out()).emit()
==>v[3]
==>v[2]
==>v[4]
==>v[5]
==>v[3]
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:
gremlin> g.V(1).repeat(out()).emit(__.not(outE()))
==>v[3]
==>v[2]
==>v[5]
==>v[3]
gremlin> g.V(1).repeat(out()).emit(__.not(outE()))
==>v[3]
==>v[2]
==>v[5]
==>v[3]
There are some duplicates in the case of the "modern" graph, you could add dedup() to the end to polish that up:
gremlin> g.V(1).repeat(out()).emit(__.not(outE())).dedup()
==>v[3]
==>v[2]
==>v[5]
gremlin> g.V(1).repeat(out()).emit(__.not(outE())).dedup()
==>v[3]
==>v[2]
==>v[5]
Dinh Phu
Dinh PhuOP2y ago
Can i ask another question? When i run this below query: "g.V().has("guid", "6203620951906330066").limit(10).out("matching").profile().toSet()" And I see step "JanusGraphVertexStep(OUT,[matching],vertex)" take a lot of time. Is there any way to solve this?
spmallette
spmallette2y ago
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.
Dinh Phu
Dinh PhuOP2y ago
ok, thank you
Dinh Phu
Dinh PhuOP2y ago
In your query
g.V(1).repeat(out()).emit(__.not(outE())).dedup()
g.V(1).repeat(out()).emit(__.not(outE())).dedup()
. 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
spmallette
spmallette2y ago
you could use optional() in this case:
g.V(1).optional(repeat(out()).emit(__.not(outE())).dedup())
g.V(1).optional(repeat(out()).emit(__.not(outE())).dedup())
Dinh Phu
Dinh PhuOP2y ago
oh, thank you
Want results from more Discord servers?
Add your server