How to retrieve specific Vertex with Id given by traversal?

1,703 views
Skip to first unread message

Thomas Driessen

unread,
Jun 12, 2020, 6:26:43 AM6/12/20
to Gremlin-users
Hi,

is there a way in gremlin to do something like this:

g.V(__.someIDTraversal())

In my usecase some property values have a metaproperty that holds the Id of the datasource vertex the respective property value is coming from and I would like to navigate to the vertex of this datasource within one query,.
I would NOT like to do two queries, where I first retrieve the Id and then do another query where I use the Id to get the vertex I want, but instead would like to do something like this:

g.V(someId).properties().properties("DatasourceId").value().V(__.identity());

Is something like this even possible in gremlin? Or should I do it another way which is better suited?

Any advice is appreciated :)

Kind regards,
Thomas

Stephen Mallette

unread,
Jun 12, 2020, 7:51:34 AM6/12/20
to gremli...@googlegroups.com
I would probably consider modeling my graph differently for this sort of thing. You basically have a situation where "a property has an edge to a vertex" and if you need such a use case I'd wonder if it could be remodeled to promote that property to a vertex so that you could have a proper edge to traverse. In that way you traverse the graph a bit more naturally. I'd also be wary of using vertex identifiers this way as some graphs do not guarantee that ids stay the same between restarts.

Answering your question more directly though, you can do what you are asking:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> v = g.addV('person').property('name','adam').
......1>       addV('datasource').property('name','ds').next()
==>v[2]
gremlin> g.V().has('person','name','adam').properties('name').property('datasource',v.id()).iterate()
gremlin> g.V().has('person','name','adam').
......1>   properties('name').values('datasource').as('edge').
......2>   V().where(id().as('edge')).
......3>   elementMap()
==>[id:2,label:datasource,name:ds]

but I don't think that where(id().as('edge')) will optimize well for all graphs. A form of has() would be better but the form I wanted to experiment with didn't work: 

gremlin> g.V().has(id, is(2L))
gremlin> g.V().has('name', is('ds'))
==>v[2]

seems like a bug of some sort. I'm still looking into that and I'm not completely sure you'd get an optimized query with it anyway without some more tests. Anyway, I created an issue for it:


For now, I'd say that two traversals would be faster than one and that you should strongly consider using an actual edge rather than a meta-property as an edge.



--
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/8e72d01c-ad13-40ee-ad93-d0f33579c236o%40googlegroups.com.

Thomas Driessen

unread,
Jun 13, 2020, 6:32:58 AM6/13/20
to gremli...@googlegroups.com
Hi Stephen,

first of all: thanks for your extensive answer! It's alway a pleasure to read those, as they are understandable also by beginners like me. Thanks for the effort you put into these.

Second: Your suggestion worked like a charm, i.e., V().where(__.id().as("someLabel")). However, is this type of usage of 'as' documented somewhere? It's a little bit counterintuitive for me as I used 'as' up until now only for putting a label to a step.

Regarding your suggestion elevating attributes to vertices: We do not have an edge to a datasource per attribute, but per attribute value, i.e., each value can come from a different  datasource. I'm not sure if elevating attributes and attribute values to vertices would create too much complexity, but I will keep it in mind. Maybe other queries we have might get easier too by this ;)

Kind regards,
Thomas

You received this message because you are subscribed to a topic in the Google Groups "Gremlin-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gremlin-users/ZG6yqE6F9qk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CAA-H43-nJ-cxJVigwAUujM_OFR6Ksgqx3zEHfc4XK%3DfO96mnwQ%40mail.gmail.com.

Kelvin Lawrence

unread,
Jun 13, 2020, 7:19:06 AM6/13/20
to Gremlin-users
On the topic of the different uses of the as() step. I do have some notes on it here [1] but I should probably make it easier to find.

[1] http://www.kelvinlawrence.net/book/PracticalGremlin.html#patternwhere

Cheers,
Kelvin

Thomas Driessen

unread,
Jun 15, 2020, 4:58:23 AM6/15/20
to gremli...@googlegroups.com
Hi Kelvin,

thanks! That made the usage of 'as' much easier to understand for me :)
BTW: Your book is awesome! It helped me a lot getting started with gremlin!

Kind regards,
Thomas

--
You received this message because you are subscribed to a topic in the Google Groups "Gremlin-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gremlin-users/ZG6yqE6F9qk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gremlin-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages