.select("") and .as("") behavior with .where("")

56 views
Skip to first unread message

Alberto Pelosi

unread,
Apr 18, 2018, 11:26:03 AM4/18/18
to Gremlin-users
Hi,

I have a question about .select("") and .as("") with .where("")

Suppose to have:

 g.V().hasLabel("x").as("xAlias").
    out("y").where(someCondition).select("xAlias")
   
   
Using .select("xAlias") I expect to obtain what I have labeled with "xAlias", that is all the vertex with label "x".

On the contrary I obtain all the vertex with label "x" that satisfy "out("y").where(someCondition)"

Is this a bug or I misunderstand .select()  and .as() behavior ?

Alberto

Kelvin Lawrence

unread,
Apr 18, 2018, 4:04:34 PM4/18/18
to Gremlin-users
Hi Alberto,

When you issue a query such as yours, you cause a number of traversers to be spawned. For each one that meets the criteria of the 'where' step you will get an instance of 'xAlias' as the result. So if the 'where' matches 10 times you will get 10 copies in the result.  

If you add a ".dedup()" to the end of your query you will just get one but this is not particularly useful.

The key thing is to think about how the query spawns traversers behind the scenes.

Hope that helps
Kelvin

Kelvin Lawrence

unread,
Apr 18, 2018, 4:14:24 PM4/18/18
to Gremlin-users
One more suggestion - try adding a ".path()" to the end of your query and you will hopefully see all the paths that the traversers took.

Cheers
Kelvin

Daniel Kuppitz

unread,
Apr 18, 2018, 4:30:49 PM4/18/18
to gremli...@googlegroups.com
Instead of traversing all the paths and then deduping all the xAliases, you can also do:

g.V().hasLabel("x").filter(out("y").where(...))

This is basically just a hasNext() on the out("y").where(...) part.

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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/3ddd033b-5534-4ff9-8223-9da595058fa9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Robert Dale

unread,
Apr 19, 2018, 7:33:52 AM4/19/18
to gremli...@googlegroups.com
'as()' just labels parts in a path.


The result is the fully traversed path that meets the conditions of the traversal.

Even if you do g.V().hasLabel("x"), you're not really getting all 'x' in one set. Instead you're getting each individual 'x' as its own traverser.

If you really wanted ALL 'x' in one group, you would use something like aggregate().

g.V().hasLabel("x").aggregate("xAlias").out("y").where(someCondition).select("xAlias")

Now, 'xAlias' would contain the entire set of 'hasLabel('x'). Then of course the 'out().where()' was pointless.



Robert Dale

Alberto Pelosi

unread,
Apr 19, 2018, 6:00:50 PM4/19/18
to Gremlin-users
Thank you for your answers, now it's clear.

My comprehension of "select" and "as" was not so deep.

My question arise from this use case:

- suppose you have some vertices with label "x" and a property "k"

- suppose some of them have __.out("edgeLabel1") (but not __.out("edgeLabel2"), by design)
    => __.where(__.out("edgeLabel1")). We can call them "firstSet"

- suppose some of them have __.out("edgeLabel2") (but not __.out("edgeLabel1"), by design)
    => __.where(__.out("edgeLabel2")). We can call them "secondSet"
   
   
How can I extract in one traversal the intersection between the two sets based on the value
of the "k" property?

My attemp was something like:

g.V().hasLabel("x").as("allX").
where(__.out("edgeLabel1")).as("firstSet").values("k").as("firstSetKValue").
select("allX").where(__.out("edgeLabel2")).as("secondSet").values("k").as("secondSetKValue").
select("firstSetKValue","secondSetKValue").where("firstSetKValue", P.eq("secondSetKValue"))

Now it's clear why it doesn't work, but there is a way to extract this information in one traversal?

Alberto

Robert Dale

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
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.
Message has been deleted

Daniel Kuppitz

unread,
Apr 19, 2018, 9:31:09 PM4/19/18
to gremli...@googlegroups.com
Not sure whether you're looking for the k values or the x vertices in your result, so here are 2 queries to cover both cases:

x vertices:

g.V().hasLabel("x").
  sideEffect(out("edgeLabel1").aggregate("k").by("k")).
  barrier().
  filter(out("edgeLabel2").values("k").where(within("k")))

k values:

g.V().hasLabel("x").
  sideEffect(out("edgeLabel1").aggregate("k").by("k")).
  barrier().
  out("edgeLabel2").values("k").where(within("k")).
  dedup()

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/86a7b08e-22c0-4fe4-9ce4-7dd64b9c5430%40googlegroups.com.

Alberto Pelosi

unread,
Apr 22, 2018, 10:36:21 AM4/22/18
to Gremlin-users
Thank you Daniel,

the second solution works well!
The first one, I don't know why, seems to ignore the condition "__.where(within("k")".

Thank you a lot,

Alberto
Reply all
Reply to author
Forward
0 new messages