代码之家  ›  专栏  ›  技术社区  ›  sirdan

Neo4j中节点的计数关系问题

  •  2
  • sirdan  · 技术社区  · 7 年前

    每条推特都有一个 topic 属性,该属性定义它所属的主题。

    MATCH (h:Hashtag)
    RETURN h.text AS hashtag, size( (h)<--() ) AS degree ORDER BY degree DESC
    

    我想为一个主题获取最流行的标签。

    MATCH (h:Hashtag)<--(t:Tweet{topic:'test'})
    RETURN h.text AS hashtag, size( (h)<--(t) ) AS degree ORDER BY degree DESC
    

    MATCH (h:Hashtag)
    RETURN h.text AS hashtag, size( (h)<--(t:Tweet{topic:'test'}) ) AS degree ORDER BY degree DESC
    

    而下一个需要很长时间才能运行

    MATCH (h:Hashtag), (t:Tweet)
    WHERE t.topic='test'
    RETURN h.text AS hashtag, size( (h)<--(t) ) AS degree ORDER BY degree DESC
    

    我该怎么办?谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   William Lyon    7 年前

    SIZE() 不是聚合(因此您将获得没有group by/aggregation的每一行的模式大小),但是 COUNT()

    MATCH (t:Tweet {topic:'test'})-->(h:Hashtag)
    RETURN h, COUNT(*) AS num ORDER BY num DESC LIMIT 10
    

    此查询的数量为 Tweet 节点,按分组 Hashtag .