我正在使用Titan图形数据库(v1.0),我正在寻找gremlin查询来查找超过2个用户之间的共同朋友。假设我有一个“user1”,“user2”和“user3”。我想找到这三个用户之间的共同朋友。我知道以下查询会给我“user1”和“user2”之间的共同朋友,但我怎样才能找到超过2个用户之间的共同朋友?我知道有两个用户我可以使用以下查询:
g.V(user1).both("friend").where(both("friend").is(eq(user2)))
超过2位用户怎么办?我知道我可以对所有对做同样的事情,但这不是一个有效的查询!
答案 0 :(得分:3)
match()
步骤可能是您最好的选择,因为它最容易阅读和书写。可能不是最快的。
g.V().has("name","user1").as("u1").
V().has("name","user2").as("u2").
V().has("name","user3").as("u3").
match(
__.as("u1").both("friend").as("f"),
__.as("u2").both("friend").as("f"),
__.as("u3").both("friend").as("f")
).where("f", neq("u1").and(neq("u2")).and(neq("u3"))).select("f")
以下是查询的实际应用:http://www.gremlinbin.com/bin/view/570a30f9a58c9
或者(没有中间遍历V()
&没有where()
条件)你可以这样做:
g.V().has("name","user1").both("friend").and(
both("friend").has("name","user2"),
both("friend").has("name","user3"))