0
votes

I want to visualization the data of neo4j which I need to return all the nodes and edges of some subgraph. For example in my graph there are these relationship, (:User)-[:RATED]->(:Movie)-[:HAS_GENRE]->(:Genre), (:User)-[:SIMILAIRITY]->(:User). I want to show the subgraph which the center is a specified movie.

The relevant segment of this code is shown below.

    @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();
}

It looks like redundant and performace-poor. So is there are some better ways to rewrite these cypher queries.

1

1 Answers

1
votes

Try this:

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;