Projecting all values in Gremlin

694 Views Asked by At

I would like to concatenate all the values of two related vertices in Gremlin.

Given the following:-

g.addV('Path').as('1').
  property(single, 'Source', 12345).
  addV('PathState').as('2').
  property(single, 'Length', 201).
  property(single, 'Tag', 'TestTag').
  addE('state').from('1').to('2')

I understand I can write a Projection, using named values, like this:

g.V().hasLabel('Path')
.project("Id", "Label", "Length", "Tag")
.by(id)
.by(label)
.by(out('state').values("Length"))
.by(out('state').values("Tag"))

I'm looking for a more generic solution that I can use where the connected 'State' vertices can have different properties.

Is there a generic way to concatenate all the properties of the connected 'state' vertex to the Path one?

I'm using C# Gremlin.Net to try to build a generic method for retrieving 'Vertex with connected StateVertex', so I have a fallback of using reflection to build up the 'by()' bits of a traversal, but it feels like there should be a simpler way.

1

There are 1 best solutions below

0
On BEST ANSWER

I think you can get the sort of information you are looking for by using the path().by(elementMap()) or path().by(valueMap()) steps in your query like this:

gremlin> g.V().hasLabel('Path').out('state').path().by(elementMap())
==>[[id:0,label:Path,Source:12345],[id:2,label:PathState,Length:201,Tag:TestTag]]

If you're looking to have these values returned in a single Map you could achieve this through some more complex manipulation in the by() modulators like this:

gremlin> g.V().hasLabel('Path').out('state').path().by(project('id', 'label').by(id()).by(label())).by(valueMap()).unfold().unfold().fold()
==>[id=0,label=Path,Length=[201],Tag=[TestTag]]

However, this seems potentially problematic as I am not sure what you are expecting to do with duplicate values.