I would like to return all vertex's that have a certain edge and the date an edge was added but I'm just not getting my head around it.
i.e.
g.V().out("likes").map{it -> it.dateLiked = it.outE("likes").property("addedOn"); it;}
In SQL terms I can think of it like;
SELECT item.*,
edge.addedOn
FROM item_table item,
edge_mapping_table edge
WHERE item.id = edge.item_id
Here's one way you might do it using the "modern" toy graph as an example:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().outE('knows').as('e').
inV().as('v').
select('e','v').by('weight').by(valueMap())
==>[e:0.5,v:[name:[vadas],age:[27]]]
==>[e:1.0,v:[name:[josh],age:[32]]]
So, instead of getting the "addedOn" from the "likes" edge and the properties of the "item" vertex, I want to get the "weight" from "knows" edge and the properties of the "person" vertex. In the first line, we traverse out on the "knows" edge and label it as 'e', then we traverse to the adjacent in
person vertex and label it as 'v'. Finally we select the values of those two labels and apply by
transforms on them. The by
are applied in round-robin fashion so the first by
is applied to 'e' and the second one is applied to the 'v'.
The use of valueMap
is a bit of a convenience, but typically you would know the data that you wanted retrieved from the vertex just as you knew the data you would retrieve from the edge (i.e. the "weight"). To make for a slightly nicer output without all the embedded Map
then you could do:
gremlin> g.V().outE('knows').as('w').
inV().as('a','n').
select('w','a','n').
by('weight').by('age').by('name')
==>[w:0.5,a:27,n:vadas]
==>[w:1.0,a:32,n:josh]