【问题标题】:how to return the collection of multi node or edge in neo4j using cypher如何使用密码在neo4j中返回多节点或边缘的集合
【发布时间】:2015-04-07 00:25:55
【问题描述】:

我想可视化 neo4j 的数据,我需要返回某个子图的所有节点和边。 例如在我的图表中有这些关系,(:User)-[:RATED]->(:Movie)-[:HAS_GENRE]->(:Genre), (:User)-[:SIMILAIRITY]->(:用户)。我想显示以指定电影为中心的子图。

这段代码的相关部分如下所示。

    @Query("match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) where m.id={0} return  id(u) as id, case labels(u)[0] when 'Genre' then u.name when 'User' then u.id when 'Movie' then u.title end as caption, labels(u)[0] as type "
        + "union "
        + "match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) where m.id={0} return id(m) as id, case labels(m)[0] when 'Genre' then m.name when 'User' then m.id when 'Movie' then m.title end as caption, labels(m)[0] as type "
        + "union "
        + "match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) where m.id={0} return id(g) as id, case labels(g)[0] when 'Genre' then g.name when 'User' then g.id when 'Movie' then g.title end as caption, labels(g)[0] as type")
    List<NodeInfo> findMovieRelevantNodes(int movieId);

    @Query("match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) where m.id={0} return id(u) as source,type(r) as type, id(m) as target, case type(r) when 'HAS_GENRE' then r.probability when 'SIMILARITY' then r.similarity when 'RATED' then r.rate end as caption "
        + "union "
        + "match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) where m.id={0} return id(m) as source,type(hg) as type, id(g) as target, case type(hg) when 'HAS_GENRE' then r.probability when 'SIMILARITY' then r.similarity when 'RATED' then r.rate end as caption")
    List<EdgeInfo> findMovieRelevantEdges(int movieId);

@QueryResult
public interface NodeInfo {
    @ResultColumn("id")
    int getId();

    @ResultColumn("caption")
    String getCaption();

    @ResultColumn("type")
    String getType();
}

@QueryResult
public interface EdgeInfo {
    @ResultColumn("source")
    int getSource();

    @ResultColumn("type")
    String getType();

    @ResultColumn("target")
    int getTarget();

    @ResultColumn("caption")
    String getCaption();
}

它看起来像多余且性能不佳。那么有没有更好的方法来重写这些密码查询。

【问题讨论】:

    标签: neo4j cypher spring-data-neo4j


    【解决方案1】:

    试试这个:

    match (u:User)-[r:RATED]->(m:Movie)-[hg:HAS_GENRE]->(g:Genre) 
    where m.id={0} 
    return  {id:id(u), caption:u.id} as user, 
            {id:id(m), caption:m.title} as movie, 
            {id:id(g), caption:g.name} as genre;
    

    【讨论】:

    • 那不行。它返回一个像这样的地图列表“{genre={id=5, caption=Comedy, type=Genre}, movie={id=22, caption=Toy Story (1995), type=Movie}, user={id =19,标题=1,类型=用户}}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多