Search code examples
gremlintinkerpop

How does 'as' work when used inside 'where'?


Learning gremlin and looking at examples I see the use of as inside of where steps to match against a previously labeled step.

For example, from official recipes page, a vertex is labeled 'v' with as, later on inside a coalesce it looks for an inE where the outV equals to 'v', but it does so using as again:

gremlin> g.V().has('person','name','vadas').as('v').
           V().has('software','name','ripple').
           coalesce(__.inE('created').where(outV().as('v')),
                    addE('created').from('v').property('weight',0.5))

If I were to just read the reference documentation this query would look to me like they are just labeling that outV step as 'v' and nothing more.

This functionality of as doesn't seem to be documented in the reference documentation, and only shows in the examples with no special mention of it.

Does the previous query achieve the same as the following?

gremlin> g.V().has('person','name','vadas').as('v').
           V().has('software','name','ripple').
           coalesce(__.inE('created').where(outV().where(eq('v'))),
                    addE('created').from('v').property('weight',0.5))

Is this just a lack of documentation, or is there something I'm missing?

Is saying as has a special meaning inside 'where', correct? Or is there something else to take into account?


Solution

  • Yes - as() has special meaning inside where(). It is described briefly as part of the select()/where() usage (see item 3) and in the match()/where() usage. Basically when you write your query that way you are using where() with its variable binding form and is equivalent to the more direct form that you demonstrate in your second query. Personally, I tend to find the binding form a bit less readable, but that depends on the nature of the filter I guess and sometimes it represents the most clear way to structure the query.