Naveen Raj
Naveen Raj

Reputation: 125

Gremlin query to traverse JanusGraph with step count until leaf vertex

I have a JanusGraph with vertices containing property key "ID" and edges labelled as "ML_EDGE", containing property key "STRENGTH".

I have to start from a vertex and traverse the graph based on the edges which has highest the "STRENGTH".

The following query works fine.

g.V().has('ID','id').repeat(outE("ML_EDGE").has("STRENGTH",gt(0)).limit(1).inV()).times(3);

But I have to stop traversal when a leaf vertex in encountered. So I made some changes.

g.V().has('ID','id').repeat(outE("ML_EDGE").has("STRENGTH",gt(0)).limit(1).inV()).until(or(loops().is(eq(2)),outE().count().is(eq(0))))

This query returns empty result.

Also, with the first query, I tried replacing times() with until() and got the query, g.V().has('ID','id').repeat(outE("ML_EDGE").has("STRENGTH",gt(0)).limit(1).inV()).until(loops().is(eq(1)));

It doesn't work either.

I am a newbie to JanusGraph and I am stuck. Any help to fix this would be life-saving.

Upvotes: 1

Views: 143

Answers (2)

Riya
Riya

Reputation: 94

g.V().has('ID', 'id').repeat(optional(outE('ML_EDGE').has('STRENGTH', gt(0)).limit(1).inV())).times(10)

Use of optional() in repeat() solved the issue.

Hope this would solve the problem.

Upvotes: 1

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

Generally, I would write the query in a similar way to you. Something like:

g.V().has('ID','id').
      repeat(outE("ML_EDGE").has("STRENGTH",gt(0)).limit(1).inV()).
      until(not(outE()).or().loops().is(2))

I don't have JanusGraph running right this minute, but I did a test using Amazon Neptune and the air-routes data set. This worked for me.

g.V().has('code','YPO').
      repeat(outE("route").has("dist",gt(0)).limit(1).inV()).
      until(not(outE()).or().loops().is(2)).
      path().
        by('code').
        by('dist')

and returned

path[YPO, 188, YAT, 55, ZKE]

If this still does not work in your case, it could be something unique to JanusGraph, but that seems a bit unlikely. I would normally also suggest making sure the data does have such a path from your starting node, but the times(3) test you did seems to imply it does.

Upvotes: 0

Related Questions