如何 return 在 Neo4j 中使用 cypher 收集多节点或边
how to return the collection of multi node or edge in neo4j using cypher
我想可视化我需要 return 某些子图的所有节点和边缘的 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();
}
看起来多余而且性能很差。那么是否有一些更好的方法来重写这些密码查询。
试试这个:
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;
我想可视化我需要 return 某些子图的所有节点和边缘的 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();
}
看起来多余而且性能很差。那么是否有一些更好的方法来重写这些密码查询。
试试这个:
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;