How Gremlin query same sql like for search feature

前端 未结 7 903
栀梦
栀梦 2020-12-31 15:12

Im using OrientDB type graph. I need syntax of Gremlin for search same SQL LIKE operator

LIKE \'search%\' or LIKE \'%search%\'

I\'ve check

7条回答
  •  有刺的猬
    2020-12-31 15:15

    I know I'm way late on this, but I was trying to figure this out as well and hope it helps someone out there. This is implemented in Java. TinkerPop version 3.2.5. I have no idea how to do this without writing some Java.

    1) Create an enum that implements BiPredicate. (Note, this predicate allows wildcards and is case insensitive.)

    public enum StringCompare implements BiPredicate {
    wildcard {
            @Override
            public boolean test(final Object first, final Object second) {
    
                String str = first.toString();
                String regex = second.toString();
    
                Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(str);
    
                return matcher.matches();
            }
        }
    }
    

    2) Create your regular expression following the Java rules. I found this link pretty helpful. http://www.developer.com/java/data/using-java-regular-expressions.html

    3) Use the has() method, pass it the property key, and create a new P object.

    String regex = "Mar.*"; //2
    GraphTraversal gtv = g.V().has("name", new P<>(StringCompare.wildcard, regex)); //3
    

提交回复
热议问题