【问题标题】:Get all the actors that played in a movie that all my friends liked获取我所有朋友都喜欢的电影中扮演的所有演员
【发布时间】:2017-02-24 11:01:51
【问题描述】:

我正在玩 TinkerPop,但我一直在纠结这个问题:我想从我所有朋友都喜欢的电影中找到所有演员(换句话说,找到我朋友喜欢的常见电影并获得在这些电影中扮演的所有演员的名字)

到目前为止我尝试了什么:

g.V(v1).out("friend").out("like").in("play_in").values("name")

返回在至少我的一个朋友喜欢的电影中扮演的所有演员。我对 TinkerPop 很陌生,庞大的 API 不知何故让我感到困惑。

谢谢!

【问题讨论】:

    标签: titan tinkerpop tinkerpop3


    【解决方案1】:

    和往常一样,让我们​​从一个示例图表开始:

    g = TinkerGraph.open().traversal()
    g.addV(id, "user 1").as("u1").
      addV(id, "user 2").as("u2").
      addV(id, "user 3").as("u3").
      addV(id, "movie 1").as("m1").
      addV(id, "movie 2").as("m2").
      addV(id, "movie 3").as("m3").
      addE("friend").from("u1").to("u2").
      addE("friend").from("u1").to("u3").
      addE("like").from("u2").to("m1").
      addE("like").from("u2").to("m2").
      addE("like").from("u3").to("m2").
      addE("like").from("u3").to("m3").iterate()
    

    如您所见,只有movie 2 被user 1 的所有朋友点赞。回答问题的遍历如下(使用 cmets 内联):

    gremlin> g.V("user 1").                                               /* start at user 1                       */
               out("friend").aggregate("friends").                        /* collect all his friends               */
               out("like").dedup().                                       /* traverse to all the movies they liked */
               filter(
                 __.in("like").where(within("friends")).count().as("a").  /* count the number of friends who liked the movie */
                 select("friends").count(local).where(eq("a"))            /* compare to the number of total friends and      */
               )                                                          /*   filter, if the counts don't match             */
    ==>v[movie 2]
    

    现在,如果你想得到演员的名字,你只需要追加:

    .in("play_in").dedup().values("name")
    

    【讨论】:

    • 非常感谢!我想过计数,但无法弄清楚如何编写这样的查询!我打算做的事情看起来很有希望!最后一件事,你有什么理由不做这样的查询吗?
    • 很大程度上取决于您的图表。如果有很多朋友(数千+)或很多喜欢的用户,那么您可能无法获得实时结果。但是,在这种情况下,您可以预先计算捷径边缘,但这是另一个话题。
    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 2019-04-16
    • 2012-05-02
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多