QuerFinding literals that aren't presently values of a vertex property

31 views
Skip to first unread message

sco...@gmail.com

unread,
Dec 29, 2021, 11:03:32 PM12/29/21
to Gremlin-users
Good evening,

Given a list of a priori known property values (e.g., names), I'm attempting to use gremlin-python to tell me which of said values are not presently the values of a vertex property.

So for instance, I'd have

g.addV().property('name', 'Alice').addV().property('name', 'Bob').iterate()

...which gives me two vertices, as expected.

...and then suppose I want to compute that 'Charlie' is not a known name at present:

g.inject(['Alice', 'Bob', 'Charlie']).unfold().as_('nm').not_(__.V().has('name', P.eq('nm'))).toList()

...unfortunately, this simply returns the injected list with all three names, where I'd expected it to return simply ['Charlie'].  I've tried some variations on this theme, e.g., using a "where" clause and dropping the "P.eq" clause, but it provides much the same results.  Ultimately, I plan to extend the query further after I load the missing name (e.g., 'Charlie') onto my traverser.  

Any help or pointers is very much appreciated, thanks for your time!

Scott

HadoopMarc

unread,
Jan 9, 2022, 5:12:44 AM1/9/22
to Gremlin-users
Hi Scott,

Here you go:
    g.withSideEffect('x', ['Alice', 'Bob', 'Charlie']) \
        .V().values('name').aggregate('nm').cap('nm') \
        .select('x').unfold().not_(__.where(P.within('nm'))).toList()
   ['Charlie']

Of course, one can only consider this as an exercise because the query would not scale and result in a full table scan. In any practical implementation you would do something like:

    names = {'Alice', 'Bob', 'Charlie'}
    g.withSideEffect('x', names) \
        .V().values('name').where(P.within('x')) \
        .toSet().symmetric_difference(names)

so that indices can be triggered for fast lookups of vertices.

Best wishes,   Marc
Op donderdag 30 december 2021 om 05:03:32 UTC+1 schreef sco...@gmail.com:

sco...@gmail.com

unread,
Jan 9, 2022, 2:30:39 PM1/9/22
to Gremlin-users
Thanks much!  

I'll start using the withSideEffect for purposes like this.  I'd been invoking P.within(<python-list>) but I see how withSideEffect up-front can probably improve caching.

Scott

Reply all
Reply to author
Forward
0 new messages