project("p").by(__.values("a", "b") Only Outputs Single Property, Bug or Expected?
I am curious why this does not behave in the way I expected. Not a problem - solution question.
I created the following Gremlin:
g.E().
hasLabel("hiddenFrom").
inV().
hasLabel("Person").
project("Trial", "Site", "Subject ID").
by(__.out().out().values("tag")).
by(__.out().values("tag")).
by(__.values("localId", "uuid"))
And in above scenario the Subject ID
output only outputs the localId
property but not uuid
property. When I change by(__.values("localId", "uuid"))
to by(__.valueMap("localId", "uuid"))
I can get the uuid
property value in the output.
A bit surprising that it did not work the way I expected but If it is an expected behavior, then I will remember so from now on. Curious if that is the case why it is designed that way.Solution:Jump to solution
i think the more specific way to answer this question is to say that
by()
modulators only grab the first item in the traversal provided to it so if you want all of them you need to provide your own reducing operator to convert all the items into a single one. typically this is done with fold()
:
by(__.values("localId", "uuid").fold())
e...2 Replies
Answer:via GPT-4:
The behavior you observed is indeed expected in Gremlin, and understanding why requires a bit of insight into how the Gremlin traversal language works.
In Gremlin, the values step is used to retrieve the values of the specified property keys of the elements in the current traversal. If more than one property key is specified, values will return the values of all those properties for each element, but it will still return them as separate items in the traversal. In other words, values("localId", "uuid") will return a traversal with the values of both localId and uuid properties, but they will be separate in the traversal stream.
Here's an example to illustrate this:
g.addV('Person').property('localId', 1).property('uuid', 'abc').next()
g.V().hasLabel('Person').values('localId', 'uuid')
This will return two separate items in the traversal: 1 and 'abc'.
On the other hand, the valueMap step is used to retrieve a map of the property keys and values for the elements in the current traversal. When you specify property keys in valueMap, it returns a map containing only those keys and their associated values. So, valueMap("localId", "uuid") will return a single map {localId=[1], uuid=['abc']} for each element in the traversal, which keeps both property values together in the output.
Solution
i think the more specific way to answer this question is to say that
by()
modulators only grab the first item in the traversal provided to it so if you want all of them you need to provide your own reducing operator to convert all the items into a single one. typically this is done with fold()
:
by(__.values("localId", "uuid").fold())
e