Search This Blog

Monday, 27 March 2017

Neo4j - Traversing along the directions of a Relation

Neo4j Relationships are what connect any two nodes in the graph.  To copy paste from the Neo4j guide:
Relationships organize nodes into arbitrary structures, allowing a graph to resemble a list, a tree, a map, or a compound entity — any of which can be combined into yet more complex, richly inter-connected structures.

The cool thing about Relations is that it need not be directional. We can even traverse the relation in either direction. For instance consider the Person ACTED_IN Movie relation. Here I could run a query to get all Actors from an single movie as well all movies of a single actor:
public List<Actor> getActorsInMovie(String movieName, int released);
public List<Movie> getMoviesActedIn(String actorName);
Here is the code for the two:
public List<Actor> getActorsInMovie(String movieName, int released) {
    try (Transaction tx = graphDb.beginTx()) {
      Map<String, Object> params = new HashMap<>();
      params.put("mName", movieName);
      params.put("releasedYear", released);
      Result result = graphDb.execute(
          "MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE m.title={mName} and m.released={releasedYear} RETURN a",
          params);
      if (!result.hasNext()) {
        return null;
      }
      List<Actor> actors = new ArrayList<>();
      while (result.hasNext()) {
        actors.add(extractActor((Node) result.next().get("a")));
      }
      return actors;
    }
The query here is : MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE m.title={mName} and m.released={releasedYear} RETURN a and the output on calling this method:
Movie [ id : 154, title : Something's Gotta Give, released : 2003, tagline : null]
Movie [ id : 100, title : Johnny Mnemonic, released : 1995, tagline : The hottest data on earth. In the coolest head in town]
Movie [ id : 87, title : The Replacements, released : 2000, tagline : Pain heals, Chicks dig scars... Glory lasts forever]
Movie [ id : 3, title : The Devil's Advocate, released : 1997, tagline : Evil has its winning ways]
Movie [ id : 2, title : The Matrix Revolutions, released : 2003, tagline : Everything that has a beginning has an end]
Movie [ id : 1, title : The Matrix Reloaded, released : 2003, tagline : Free your mind]
Movie [ id : 0, title : The Matrix, released : 1999, tagline : Welcome to the Real World]
Similar is the method to retrieve actors with a slight difference in query:
MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name={aName} RETURN m Here while the WHERE clause has changed the MATCH clause is the same and RETURN now fetches the movie nodes instead of actor nodes.
public List<Movie>getMoviesActedIn(String actorName) {
    try (Transaction tx = graphDb.beginTx()) {
      Map<String, Object> params = new HashMap<>();
      params.put("aName", actorName);
      Result result = graphDb.execute(
          "MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WHERE a.name={aName} RETURN m",
          params);
      if (!result.hasNext()) {
        return null;
      }
      List<Movie> movies = new ArrayList<>();
      while (result.hasNext()) {
        movies.add(extractMovie((Node)result.next().get("m")));
        }
      return movies;
    }
The output is as below:
Actor [ title : Dina Meyer, yearBorn : 1968 ]
Actor [ title : Ice-T, yearBorn : 1958 ]
Actor [ title : Keanu Reeves, yearBorn : 1964 ]
Actor [ title : Takeshi Kitano, yearBorn : 1947 ]

7 comments:

  1. Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
    Housekeeping Services in Chennai

    ReplyDelete
  2. I am not sure the place you are getting your information, however good topic.I needs to spend some time studying more or understanding more.Thank you for wonderful information I was in search of this info for my mission.

    Staffing Companies in Chennai
    Recruitment Franchise in Chennai
    Staffing Companies in Bangalore

    ReplyDelete
  3. Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.

    Java Training in Chennai

    ReplyDelete
  4. This is extremely great information for these blog!! And Very good work. It is very interesting to learn from to easy understood. Thank you for giving information. Please let us know and more information get post to link.

    Oracle Training in Chennai

    ReplyDelete

  5. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete
  6. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  7. Really, these quotes are the holistic approach towards mindfulness. In fact, all of your posts are. Proudly saying I’m getting fruitfulness out of it what you write and share. Thank you so much to both of you.
    RFID Solutions
    Athletic Management Software
    CCTV Camera Dealers in Chennai
    CCTV Camera Installation in Chennai

    ReplyDelete