0

I am running into curious problem.

enter image description here

In graph above,I can't run repeat/until due to memory overload issue and vertices names changing at various level. I am trying direct edge traversal through each step.

However, I see that the query is only showing paths with max traversal depth.

Graph:

g.addV(‘color-group’).property(id,1).property(single, ‘Color’, ‘primary’).next()
    g.addV(‘primary-color’).property(id,2).property(single, ‘Color’, ‘red’).next()
    g.addV(‘primary-color’).property(id,3).property(single, ‘Color’, ‘blue’).next()
    g.addV(‘primary-color’).property(id,4).property(single, ‘Color’, ‘yellow’).next()
    g.addV(‘secondary-color’).property(id,5).property(single, ‘Color’, ‘red-10’).next()
    
    g.V(1).addE('links').to(g.V(2)).next()
    g.V(1).addE('links').to(g.V(3)).next()
    g.V(1).addE('links').to(g.V(4)).next()
    g.V(4).addE('links').to(g.V(5)).next()
    

Query:

>>> g.V().hasLabel('color-group').has('Color', 'primary').as_('color_group').outE().inV().outE().inV().path().next()

Result:

 path[v[1], e[40][1-links->4], v[4], e[41][4-links->5], v[5]] 

so above its skipping paths from primary -> blue and primary -> yellow and only showing paths from primary -> red -> red-10.

I need all of the paths (without using repeat/until).

Any ideas?
Thanks.

Anil_M
  • 10,893
  • 6
  • 47
  • 74

1 Answers1

1

The reason you do not get all of the paths is because only one of them satisfies the "2-hop" requirement. For this specific case you could use optional, but for paths of unknown depth this might become complex.

In the Gremlin Console using this version of the data (modified so that it loads on the 3.6.2 level of Gremlin and with the quotes fixed) ...

g.addV('color-group').property(id,1).property(single, 'Color', 'primary')
g.addV('primary-color').property(id,2).property(single, 'Color', 'red')
g.addV('primary-color').property(id,3).property(single, 'Color', 'blue')
g.addV('primary-color').property(id,4).property(single, 'Color', 'yellow')
g.addV('secondary-color').property(id,5).property(single, 'Color', 'red-10')
g.V(1).addE('links').to(V(2))
g.V(1).addE('links').to(V(3))
g.V(1).addE('links').to(V(4))
g.V(4).addE('links').to(V(5)) 

The query can be re-written as:

g.V().hasLabel('color-group').has('Color', 'primary').as_('color_group').
      outE().inV().
      optional(outE().inV()).
      path()

When run:

gremlin> g.V().hasLabel('color-group').has('Color', 'primary').
......1>       outE().inV().
......2>       optional(outE().inV()).
......3>       path()   

==>[v[1],e[12][1-links->2],v[2]]
==>[v[1],e[13][1-links->3],v[3]]
==>[v[1],e[14][1-links->4],v[4],e[15][4-links->5],v[5]] 

With properties and labels for the nodes and edges:

gremlin> g.V().hasLabel('color-group').has('Color', 'primary').
......1>       outE().inV().
......2>       optional(outE().inV()).
......3>       path().
......4>         by('Color').
......5>         by(label) 

==>[primary,links,red]
==>[primary,links,blue]
==>[primary,links,yellow,links,red-10]   

Side note: The sample graph does not quite match the diagram. These results are correct for the sample data provided.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Ah, I did read about optional , but didn't quite get it.thanks. I've two followup questions. (1) How can i get properties for respective vertex/edge with path(). (2) How would I convert the path() output to standard json to be used by visualization library (I am using `gremlinpython` 3.6.1). It doesn't seem to have `GraphSonMapper`. – Anil_M Apr 06 '23 at 23:25
  • 1
    To get the properties for each node and edge in the `path` result, you could do something as simple as `path().by(valueMap())` or `path().by(elementMap())` just keep in mind that can increase the result size significantly if there are a lot of results. For the JSON question, it might be easier to ask in a separate post as it will likely require some discussion and a longer answer. – Kelvin Lawrence Apr 07 '23 at 00:43
  • Hi, I asked the question about JSON conversion in this post - thx. https://stackoverflow.com/questions/75954903/turn-gremlin-query-result-with-elementmap-into-json-gremlinpython-3-6-1 – Anil_M Apr 07 '23 at 01:44
  • Thanks will take a look over there. – Kelvin Lawrence Apr 07 '23 at 14:20