Reputation: 1751
I am trying to create a Regex custom predicate for my query
I am seeing this can be done from the gremlin console as below
f = {x,y -> x ==~ y}
g.V().has('desc',test(f,/^Dal.*/)).values('desc')
however I am wondering how I can create custom predicate in a Javascript client? I am using npm package (https://www.npmjs.com/package/gremlin) and Typescript.
Upvotes: 0
Views: 291
Reputation: 14371
The example you found works because when working with a local (embedded) graph such as a TinkerGraph you can essentially create custom classes and closures in Java and/or Groovy. You can think of this as extending Gremlin locally.
However, the Gremlin JavaScript client is designed to work with a remote graph. Many hosted graph providers limit or block entirely the use of such code for reasons of security. If you have control over the Gremlin Server you are connecting to or the provider you are using allows for closures/lambdas then you may be able to take advantage of that, see [1].
If you control the Gremlin Server you are using you could potentially just add the scripts that create the custom predicates there in the configuration files. For completeness to help others who find this post I included a link to the discussion on predicates that I believe you are referring to in your question [2].
[1] https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-lambda
[2] http://www.kelvinlawrence.net/book/PracticalGremlin.html#pred
Updated 2023-03-23
Starting with Apache TinkerPop version 3.6.0, the Gremlin language does now have a TextP.regex
predicate. You can read more details here.
This allows you to write queries such as:
g.V().has('desc',regex("^Dal.*")).values('desc')
Upvotes: 1