How Gremlin query same sql like for search feature

前端 未结 7 1146
栀梦
栀梦 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:33

    As one of the committers of the SimpleGraph project i know of the solution implemented in that project

    https://github.com/BITPlan/com.bitplan.simplegraph/blob/master/simplegraph-core/src/main/java/com/bitplan/gremlin/RegexPredicate.java

    Example

    g().V().has("tosearch", RegexPredicate.regex("search.*"))
    

    RegexPredicate

    import java.util.function.BiPredicate;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.tinkerpop.gremlin.process.traversal.P;
    
    // https://groups.google.com/forum/#!topic/gremlin-users/heWLwz9xBQc
    // https://stackoverflow.com/a/45652897/1497139
    public class RegexPredicate implements BiPredicate {
        Pattern pattern = null;
        private Mode mode;
    
        enum Mode {
            FIND, MATCH
        }
    
        public RegexPredicate(String regex, Mode mode) {
            this.mode = mode;
            pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        };
    
        public RegexPredicate(String regex) {
            this(regex,Mode.FIND);
        }
    
        @Override
        public boolean test(final Object first, final Object second) {
            String str = first.toString();
            Matcher matcher = pattern.matcher(str);
            switch (mode) {
            case FIND:
                return matcher.find();
            case MATCH:
                return matcher.matches();
            }
            return false;
        }
    
        /**
         * get a Regular expression predicate
         * 
         * @param regex
         * @return - the predicate
         */
        public static P regex(Object regex) {
            BiPredicate b = new RegexPredicate(regex.toString());
            return new P(b, regex);
        }
    }
    
        

    提交回复
    热议问题