Gremlin: “The provided traverser does not map to a

2019-07-18 23:29发布

问题:

In the Modern graph, I want to get for each person the name and the list of names of software he created. So I tried the following query

g.V().hasLabel('person').project('personName','softwareNames').
                         by(values('name')).
                         by(out('created').values('name').aggregate('a').select('a'))

but I get the error

The provided traverser does not map to a value: v[2]->[VertexStep(OUT,[created],vertex), PropertiesStep([name],value), AggregateStep(a), SelectOneStep(last,a)]

The problem seems to be that vertex 2 has no "created" edges.

The query works if I run it only on vertices with at least one "created" edge, e.g. for vertex 4 ("V(4)" instead of "V()"), the result is

==>[personName:josh,softwareNames:[ripple,lop]]

How can I get an empty list of software names for vertex 2, instead of the error?

回答1:

You can simplify your Gremlin to this:

gremlin> g.V().hasLabel('person').
......1>   project('personName','softwareNames').
......2>     by('name').
......3>     by(out('created').values('name').fold())
==>[personName:marko,softwareNames:[lop]]
==>[personName:vadas,softwareNames:[]]
==>[personName:josh,softwareNames:[ripple,lop]]
==>[personName:peter,softwareNames:[lop]]

The by() modulator only does a next() on the inner traversal passed to it, so you need to reduce the results yourself - in this case fold() does that and handles the situation where you have an empty result.