.match() in Gremlin-Python

145 views
Skip to first unread message

Happy Feet

unread,
Jun 4, 2020, 8:24:46 AM6/4/20
to Gremlin-users
I have a (gremlin 3.46, conf/gremlin-server-neo4j.yaml) db which I interact with via gremlinpython

The following queries both return results:

g.V(0).as_('a').out().match(as_('b').has("groupId",1)).select('b').next()

g.V(0).as_('a').out().match(as_('b').has("groupId",2)).select('b').next()

but I am unable to get the following query to work:

g.V(0).as_('a').out().match(as_('b').has("groupId",1), as_('c').has("groupId",2)).select('b', 'c').next()

I have tried wrapping the arguments to match in a tuple or a list but neither seems to work. 
For now, I have resorted to 'rewinding' back via select('a') to fetch multiple elements in a match, but this is ugly and probably suboptimal. 

Stephen Mallette

unread,
Jun 5, 2020, 6:12:14 AM6/5/20
to gremli...@googlegroups.com
You match() with two has() on "group" would require the vertex to satisfy both of those filters and so it therefore returns no result. I simulated your traversal with the modern graph to demonstrate:

gremlin> g.V(1).as('a').out().
......1>   match(__.as('b').hasLabel('person')).
......2>   select('b')
==>v[2]
==>v[4]
gremlin> g.V(1).as('a').out().
......1>   match(__.as('c').hasLabel('software')).
......2>   select('c')
==>v[3]
gremlin> g.V(1).as('a').out().
......1>   match(__.as('b').hasLabel('person'),
......2>         __.as('c').hasLabel('software')).
......3>   select('b','c')
gremlin>

I'm not clear what result you want to achieve so I can't suggest other options. If you have a sample graph and provide some clarification on what you want to achieve, perhaps I could help further.



--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/75d5b07a-3413-4927-ab23-b5a688a1565e%40googlegroups.com.

Happy Feet

unread,
Jun 5, 2020, 7:58:42 AM6/5/20
to Gremlin-users
Hi Stephen,

I think I might have misunderstood the use of match. The query that I wanted to shorten with match() is:

g.V(0).as_('a').out()
         .has("groupId",1).as_('b')
         .select('a').out().has("groupId",2).as_('c')
         .select('b', 'c').next()

which selects all pairs of nodes (b,c) where b and c are in v(0).out() and have groupIds 1 and 2, respectively.


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

Stephen Mallette

unread,
Jun 8, 2020, 7:46:59 AM6/8/20
to gremli...@googlegroups.com
Maybe I'm oversimplifying but isn't this a case for or() which perhaps would be better written using within():

g.V().out().
  or(has("groupId",1),
     has("groupId",2))

g.V().out().has('groupId',within(1,2))



To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/8224ca79-f458-4677-aaf1-277285bb0132o%40googlegroups.com.

Happy Feet

unread,
Jun 9, 2020, 4:44:12 AM6/9/20
to Gremlin-users
Yes, or() or within() work for that example.

The fault for oversimplifying is mine: more generally I wanted the following pattern (with potentially more than just b and c):

g.V().(some initial traversal).as_('a').(some traversal from a).as('b')
.select('a').(another traversal from a).as('c')
.(some traversal that expresses a relation between b and c).
.select('a', 'b', 'c')

I thought match() would allow me to express something like this more efficiently when the number of selected nodes/relations between them balloons.

Stephen Mallette

unread,
Jun 9, 2020, 7:38:17 AM6/9/20
to gremli...@googlegroups.com
>  I thought match() would allow me to express something like this more efficiently when the number of selected nodes/relations between them balloons.

I suppose it might, but I think we're talking in really general terms here so it's hard to say exactly what you need. I wouldn't go for a match() if my traversal could be more easily expressed. Personally, I don't find myself using match() all that often. In addition, more often than not, I tend to find that:

g.V().as('a').out().as('b').....as('c')....as('z').select('a',b','c',...'z')

patterns can be re-written for better performance and improved readability, so if you have a ballooning select() situation I'd wonder if there is a better way to accomplish what you're trying to do.

 

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/52fcdc1c-0b05-48f9-ad4f-f3f87671f04co%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages