Using select in has/hasId steps

568 views
Skip to first unread message

Thomas Thomas

unread,
Jul 17, 2019, 11:47:43 AM7/17/19
to Gremlin-users
Hello,

I recently started using gremlin and I have some questions about the has/hasId steps when used with the select step.

I'm working with the Modern graph:

gremlin> graph = TinkerFactory.createModern()

==>tinkergraph[vertices:6 edges:6]


gremlin> g = graph.traversal()

==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]


gremlin> g.V(1).valueMap()

==>[name:[marko],age:[29]]



HasId step

I add a "useless" label v in order to be able to select it later on in the traversal.

g.V(1).as('v').hasId(select('v').id())


The previous traversal returns nothing but I don't understand why. As a reminder, here are the two signatures of this step:

hasId(Object id, Object... otherIds)
hasId
(P<Object> predicate)


(1) What happened?

Has step

I also used a select step in a has step:

gremlin> g.V(1).as('v').has('name', select('v').by('age'))

==>v[1]


Notice that I'm comparing the name property with the age property (just for testing).
The vertex is still returned so I guess that the following signature was used:

has(key, traversal): Remove the traverser if its object does not yield a result through the traversal off the property value.

Indeed, the traversal returns a result (the age) and therefore the filter step does not filter the vertex.

(2) I don't understand the usefulness of the key in this signature. Or I'm wrong about how it works.

(3) Last question: how to use a has step filtering on the value returned by a select...by?

Thank you

Daniel Kuppitz

unread,
Jul 18, 2019, 1:50:00 PM7/18/19
to gremli...@googlegroups.com
(1) What happened?

There's no vertex with the id select('v').id(). hasId() takes your traversal as-is, it won't evaluate the traversal, so it's basically looking for a vertex that has a traversal-id.

(2) I don't understand the usefulness of the key in this signature. Or I'm wrong about how it works.

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).as('v').has('age', math('_*2').where(gt('v')).by().by('age'))
==>v[1]

The above traversal in plain English: The current person's age times 2 has to be greater than v's age. I hope that clarifies it.

(3) Last question: how to use a has step filtering on the value returned by a select...by?

You don't, has() is for constant value comparisons. Instead, you should use where():

gremlin> g.V().as('a').both('knows').as('b').
......1>   where('a', gt('b')).
......2>     by('age').
......3>   select('a','b').
......4>     by(valueMap())
==>[a:[name:[marko],age:[29]],b:[name:[vadas],age:[27]]]
==>[a:[name:[josh],age:[32]],b:[name:[marko],age:[29]]]

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/980e3b4e-d647-4db5-96d3-6975f2a8e1f4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted
Message has been deleted

Thomas

unread,
Jul 22, 2019, 9:14:36 AM7/22/19
to Gremlin-users
Thank you Daniel, it's clearer.

Daniel Kuppitz

unread,
Jul 22, 2019, 3:22:47 PM7/22/19
to gremli...@googlegroups.com
It might be a Long id. Try g.V(28L).

Cheers,
Daniel


On Mon, Jul 22, 2019, 5:29 AM Thomas <thomas.v...@amanote.com> wrote:
Note that it is working in gremlin console:

gremlin> g.addV('person').property('name','thomas')

==>v[15]

gremlin> g.V(15)

==>v[15]


but not with gremlin-server:

gremlin> :> g.addV('person').property('name','thomas')

==>v[28]

gremlin> :> g.V(28)

gremlin>



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Thomas

unread,
Jul 23, 2019, 4:02:57 AM7/23/19
to Gremlin-users
Indeed, it is working with long id ;)

However, I'm using a custom OGM that generates requests from step instructions.
It is not easy to update that OGM so that only integers that are ids are converted to long (by appending L).

It's a pity that the server cannot be configured to generate uuid instead of long when vertexIdManager is set to ANY.
I can't set vertexIdManager to UUID either because I sometimes provide non-uuid string as id.

Stephen Mallette

unread,
Jul 23, 2019, 5:44:45 AM7/23/19
to gremli...@googlegroups.com
>  It's a pity that the server cannot be configured to generate uuid instead of long when vertexIdManager is set to ANY.

I suppose it could be. Just write your own IdManager. Just implement the interface yourself, put the jar in Gremlin Server's path, then reference it by its full class name in your configuration file.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Thomas

unread,
Jul 23, 2019, 5:52:43 AM7/23/19
to Gremlin-users
That's good to know, Stephen, thank you.
Reply all
Reply to author
Forward
0 new messages