【发布时间】: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