Search code examples
gremlingremlinpython

Gremlin-python: Select all node pairs where numerical properties are related by lt/gt


My graph has the following edges:

e[1][0--classes->1]
e[2][0--classes->2]
e[3][0--classes->3]

and nodes 1,2,3 have property classId 1,2 and 3 respectively.

I want to write a query to return all pairs (m,n) such that m.classId < n.classId. The following is my closest attempt thus far:

g.V(0).out("classes")
.as_('n').classId.as_('nid')
.select('n').in_("classes").out("classes")
.as_('m').classId.where(lte(select('nid')))
.select('m', 'n')

Unfortunately, lte expects a number while select('nid') is a traversal. I have tried various variants of inputs to both the .where and is_ traversals, but haven't had any joy.


Solution

  • You don't need to use select inside lte step. you can use the named "nid" without it.

    You were also missing by modulator:

    g.V().hasLabel('0').out('classes').as('n').
        as('nid').select('n').
      in('classes').out('classes').as('m').
      where(lte('nid')).
        by('classId').
        select('m', 'n')
    

    example: https://gremlify.com/bu789rfvme89q