Problem creating multiple vertices and then selecting the first one.

36 views
Skip to first unread message

Nick Bailey

unread,
Jul 8, 2021, 12:42:59 PM7/8/21
to Gremlin-users
I'm trying to write a traversal that upserts two vertices and then goes back and selects the first vertex that was created. For some reason after I create/find the second vertex, the tag I added to the first vertex is no longer available. 

Can anyone provide any insight into what's wrong with this traversal?

Traversal:

g.V().
    hasLabel('user').
    has('userId', 'id1').
    fold().
    coalesce(unfold(), addV('user').property(single, 'userId', 'id1')).
    as('userVertex').
    V().
    hasLabel('email').
    has('emailId', 'id1').
    fold().
    coalesce(unfold(), addV('email').property(single, 'emailId', 'id1')).
    as('emailVertex').
    select('userVertex')

Running this traversal on an empty graph returns nothing, but does successfully create both vertices the first time it runs.

Thanks in advance,
Nick

Edoardo Barp

unread,
Jul 9, 2021, 4:39:24 AM7/9/21
to Gremlin-users
Hi Nick,
Yes, that's due to fold, which is a reducing step. Read more here: http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html#rbarriers
In your case, you probably want to use `store` instead of `as`. `store` allows you to store traversal to memory so that you can use them later, while `as` just names a current traversal step, and reducing steps (such as fold) "forget" all the past steps and just keep the current state.
Do be careful since `store` saves the variable in a list, which is a bit confusing. I advise you to use `select` steps together with `toList` to see what the variables look like, I've found it a very useful debug trick.
In your case, this is what the code would look like:
g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex').

V().
hasLabel('email').
has('emailId', 'id1').
fold().
coalesce(unfold(), addV('email').property(Cardinality.single, 'emailId', 'id1')).
// example adding an edge from the current vertex email to the user vertex
addE("has_email").to(select('userVertex').unfold()).
iterate()

to understand why you need the unfold for the select userVertex, try this query:
g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex')
.fold()
.select('userVertex').unfold()
.toList()

and compare against

g.V().
hasLabel('user').
has('userId', 'id1').
fold().
coalesce(unfold(), addV('user').property(Cardinality.single, 'userId', 'id1')).
store('userVertex')
.fold()
.select('userVertex')
.toList()


PS: I'm using gremlin-python, which has some very slight syntax differences. I've tried to fix the code in "native" gremlin but there might be small mistakes

Nick Bailey

unread,
Jul 9, 2021, 11:41:06 AM7/9/21
to gremli...@googlegroups.com
Thanks so much. That explains it and that link is very helpful.

Out of curiosity, why is 'store()' not documented in the graph traversal steps on the tinkerpop site? It's referenced a bit in the examples but doesn't have it's own section.


--
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/0223e1d3-210c-483d-afac-b8937b9ca32en%40googlegroups.com.

Edoardo Barp

unread,
Jul 10, 2021, 6:09:22 AM7/10/21
to gremli...@googlegroups.com
I'm merely an average user, I'm afraid I have no answer for that! 

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/JYjBEufRT5c/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/CAGmjaMrg4tPbmXM16Ap%3Dj%3DsTM%2B1V%2Btuzd%3DKKoZj%2B3c8fTOd0LA%40mail.gmail.com.

AMIYA KUMAR SAHOO

unread,
Jul 10, 2021, 9:56:35 AM7/10/21
to gremli...@googlegroups.com
Store step is deprecated. as of 3.4.3 it has been replaced with aggregate step with scope local. 




Reply all
Reply to author
Forward
0 new messages