How gremlin has step with a regex?

2,493 views
Skip to first unread message

pengzhi...@gmail.com

unread,
Jan 6, 2017, 5:03:38 AM1/6/17
to Gremlin-users
Hello, 
       I want to filter vertex with has step, when vertex property value meets a regex, how to do it? like g.V().has("name", "^marko")  find all vertices when property 'name' start with 'marko'.

Daniel Kuppitz

unread,
Jan 6, 2017, 10:52:28 AM1/6/17
to gremli...@googlegroups.com
TinkerPop doesn't support RegEx matching out of the box. Some implementations (Titan and DSE Graph) do have some extra predicates though.
However, it's easy to implement your own predicate:

gremlin> regexBiPredicate = new java.util.function.BiPredicate<String, String>() {
......1>   boolean test(String value, String pattern) {
......2>     return value ==~ pattern
......3>   }
......4> }
==>groovysh_evaluate$1@43f9dd56

gremlin> regex = {
......1>   new P(regexBiPredicate, it)
......2> }
==>groovysh_evaluate$_run_closure1@354e7004

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().has("name", regex(/mark.*/))
==>v[1]

If startsWith(...) is all you need, you can also use a simpler solution:

gremlin> g.V().has("name", between("marko", "markp"))
==>v[1]

Cheers,
Daniel



On Fri, Jan 6, 2017 at 11:03 AM, <pengzhi...@gmail.com> wrote:
Hello, 
       I want to filter vertex with has step, when vertex property value meets a regex, how to do it? like g.V().has("name", "^marko")  find all vertices when property 'name' start with 'marko'.

--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/b3b327cf-93f4-4000-ba59-d74e362b87b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Praneeth Dogiparthi

unread,
Feb 4, 2019, 3:05:58 PM2/4/19
to Gremlin-users
Thanks for the solution it worked but what if we filter over multiple properties 

for example :  g.V().has("name", between("marko", "markp")) . this only returns vertices matching marko for property name.

But what if we need to get all vertices who firstname or lastname that contains marko . the above example only searches for only one property but we need to search for whether marko is present in firstname or lastname both properties. can you help. Thanks in advance. 
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

Daniel Kuppitz

unread,
Feb 4, 2019, 4:56:24 PM2/4/19
to gremli...@googlegroups.com
Well, you can use the or() step:

g.V().or(has("firstname", "marko"), has("lastname", "marko"))

 ...or just execute 2 traversals in parallel. Depending on your graph provider and how these properties are indexed, one approach may perform much better than the other.

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages