mmr25
mmr25

Reputation: 93

Pattern matching in gremlin

I have a graph with labels users and i want to search the users by name this is the current implementation

wg.addV("users").property("p1", "user1").property("p2", "test").next();

now i need to search for the users having their property1 or property2 starts with the letters as user type in search.

if user typed "u" i need to get the users whose p1 or p2 starts with "u". if user typed "use" i need to get the users whose p1 or p2 starts with "use".

and i need to display in relevant order and limit to 10 results.

this is the current implementation.

g.V().or(has("users", "p1", between("use", "use" + "z")),
                    has("users", "p2", between("use", "use" + "z")))
            .limit(10))

with this approach im able to the users but its not relevant and it is not including the users that match the exact query and order by p1. Thanks in advance.

Upvotes: 1

Views: 443

Answers (1)

noam621
noam621

Reputation: 2856

Since the release of TinkerPop 3.4.0, Gremlin also supports simple text predicates

In this case, you should use startingWith.

g.V().or(
    has('users', 'firstname', startingWith('use')),
    has('users', 'lastname', startingWith('use'))
  ).limit(10)

example: https://gremlify.com/sdgnafh8md

Upvotes: 1

Related Questions