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
S
spmallette394d 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]
DP
Dinh Phu394d 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?
S
spmallette394d 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.
DP
Dinh Phu394d ago
ok, thank you
DP
Dinh Phu358d 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
S
spmallette357d 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())
DP
Dinh Phu357d ago
oh, thank you
Want results from more Discord servers?
Add your server
More Posts
Does Gremlin do DFS or BFS?Does Gremlin perform a Depth First Search (DFS) or Breadth First Search (BFS) when traversing?Self-service roles for providers and areas of expertiseWe have roles for providers right now as listed in #roles - they are handed out by moderators. So faMore elaborate sack examples in the docsThere are a few examples for the `sack` step, but it would be good to have a more concrete one. For Isolated vertices vs connected vertices with no join benefitIs there any downside to storing an isolated vertex with references to other nodes? Creating relatioSubgraph Strategy with vertexProperties + project().by("field name") = crashRunning the following query: `g.withStrategies(new SubgraphStrategy(vertexProperties: constant(true)Custom MutationListener on TransactionHello everyone, I'm a beginner regarding tinkerpop and i'm trying to fire my custom listener after Gremlin Query to give all related items in versioned graphI am working on a requirement where I need to store all version of a record in a Graph Database say Order group count result alphabeticallyHi! Given the following query and results in the enclosed image: how would I sort the labels alphabTransactions - tx.commit vs tx.closeI have a question related to transactions. I'm having issues with tx.commit() hanging locally when rExtracting the ProjectStep of a GraphTraversal instance during unit testing**Tl;dr** Given an instance of `GraphTraversal<?, Map<String, Object>>`, is it possible to extract tWhen can we get a non-RC release for Python 3.11 support in Production Envs?There was a bug where the version of aiohttp was too strict and blocked Python 3.11 support. ( httpsSubgraph query returns String instead of TinkerGraph Object in Fluent Java APIHi guys. I am running the following query in the console and it works fine: `g.V().has('user', 'id'Multiple Graphs in Gremlin Server?How do I manage multiple graphs? Is there an option where I can select which graph to use for query Has anyone else encountered "NoSuchElementException" when calling getLeafTrees() on a tree objectDid a little bit of debugging and it seems that the issue has to do with a cast to Tree before calliThe query gets slower as the number of vertices that already exist in JanusGraph gets bigger and bigWhen i start JanusGraph and perform any query like : - g.V().has(properties).limit(10).toList() - g.Is there a limitation on Neptune HTTP API endpoint compatibility when using a proxy and IAM Auth?Hi, Got a weird one today. I'm working on bringing full compatibility for the use of proxies frontiPreventing Janusgraph crash on timeoutAccording to this: https://stackoverflow.com/questions/61985018/janusgraph-image-stop-responding-aftWay to update static vertexhttps://docs.janusgraph.org/schema/advschema/#static-vertices I read document about TTL vertex. And Dotnet best practice: converting Vertex properties to ModelA very common task in Dotnet is to convert a stored entity into a Model class. How is this best accoWhat is the use of adding type information to e.g ElementMap<type> in Gremlin.Net?Consider the query and output in the attached image: What TYPE could be placed inside the `Elemement