ashissl
ashissl

Reputation: 377

Filter Gremlin traversal for match a vertex seen earlier in the traversal

I'm unsure how to filter by equality to a vertex stored earlier in the traversal with "as" in gremlinJS. Here is a contrived example to show exactly what I mean. This query checks for users that a given user LIKES that are not currently IS_FRIENDS_WITH that user.

await g.V(userId).as('user').out("LIKES").where(__.out('IS_FRIENDS_WITH')).hasId(userId)

I would like to write the above as the below, which does not work, as hasId does not receive a string. Note the change in hasId.

await g.V(userId).as('user').out("LIKES").where(__.out('IS_FRIENDS_WITH')).hasId(__.select('user'))

This comes up in various situations. In one example, I have a builder function for constructing a query from creating a bidirectional 'IS_FRIENDS_WITH' edge between two vertices. The current version creates this between two hard coded edges.

query = query
      .coalesce(
        __.outE('IS_FRIENDS_WITH').where(__.inV().hasId(targetUserId)),
        __.addE('IS_FRIENDS_WITH')
          .to(__.V(targetUserId))
          .property('createdAt', new Date().toISOString()),
      )
      .property('updatedAt', new Date().toISOString())
      .V(targetUserId)
      .coalesce(
        __.outE('IS_FRIENDS_WITH').where(__.inV().hasId(sourceUserId)),
        __.addE('IS_FRIENDS_WITH')
          .to(__.V(sourceUserId))
          .property('createdAt', new Date().toISOString()),
      )
      .property('updatedAt', new Date().toISOString());

    return query;

I would like to create a new version that creates the edge between a target edge and the current vertex being traversed, as written below, which again errors at the hasId(__.select('source')) step.

query = query
      .coalesce(
        __.outE('IS_FRIENDS_WITH').where(__.inV().hasId(targetUserId)),
        __.addE('IS_FRIENDS_WITH')
          .to(__.V(targetUserId))
          .property('createdAt', new Date().toISOString()),
      )
      .property('updatedAt', new Date().toISOString())
      .V(targetUserId)
      .coalesce(
        __.outE('IS_FRIENDS_WITH').where(__.inV().hasId(__.select('source'))),
        __.addE('IS_FRIENDS_WITH')
          .to(__.select('source'))
          .property('createdAt', new Date().toISOString()),
      )
      .property('updatedAt', new Date().toISOString());

    return query;

So, how can I check for equality to a vertex reference earlier with .as('identifier')?

Upvotes: 0

Views: 28

Answers (0)

Related Questions