Pattern matching in gremlin 在Gremlin中的模式匹配

huangapple go评论74阅读模式
英文:

Pattern matching in gremlin

问题

我有一个带有标签 "users" 的图形,我想按名称搜索用户,这是当前的实现:

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

现在我需要按照用户输入的搜索条件来搜索具有属性1或属性2以某些字母开头的用户。

如果用户输入 "u",我需要获取属性1或属性2以 "u" 开头的用户。
如果用户输入 "use",我需要获取属性1或属性2以 "use" 开头的用户。

并且我需要按相关顺序显示结果,并限制为 10 条结果。

这是当前的实现:

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

通过这种方法,我能够获取用户,但结果不够相关,而且不包括与精确查询匹配并按 p1 排序的用户。提前感谢。

英文:

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.

答案1

得分: 1

自 TinkerPop 3.4.0 发布以来,Gremlin 还支持简单的文本谓词。

在这种情况下,您应该使用 startingWith

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

示例:https://gremlify.com/sdgnafh8md

英文:

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

huangapple
  • 本文由 发表于 2020年8月6日 13:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63277267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定