Total Pageviews

2018/10/09

[Neo4j] Cypher example

以下有個朋友與電影的關係圖:


若想找出 John Johnson 的朋友:
// 找出 John Johnson 的朋友
match(u:User) where u.name="John Johnson"
match (u)-[r:IS_FRIEND_OF]-(n:User)
return n as friends

Neo4j browser 執行結果:



若想找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影:
// 找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影
match(u1:User)-[:HAS_SEEN]->(m1:Movie)<-[:HAS_SEEN]-(u2:User)-[:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2))
return u1 as user, collect(m2.name) as movies

Neo4j browser 執行結果:



若想找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影:
// 找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影
match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2))
return m2 as movie, r2.stars as stars

Neo4j browser 執行結果:




若想找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星:
// 找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星
match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2)) and r2.stars > 3
return m2 as movie, r2.stars as stars


Neo4j browser 執行結果:

於 Spring Data 中, repository class 定義如下:
package neo4j.springdata.repository;

import java.util.List;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import neo4j.springdata.vo.User;
import neo4j.springdata.vo.result.UserQueryResult;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

    @Query(" match(u:User) where u.name={name} " + " match (u)-[r:IS_FRIEND_OF]-(n:User) " + " return n as friends ")
    List<UserQueryResult> findFriends(@Param("name") String name);

    @Query(" match(u1:User)-[:HAS_SEEN]->(m1:Movie)<-[:HAS_SEEN]-(u2:User)-[:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) "
            + " return u1 as user, collect(m2.name) as movies ")
    List<UserQueryResult> findRecommendedMovies(@Param("name") String name);

    @Query(" match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) " + " return m2 as movie, r2.stars as stars")
    List<UserQueryResult> findUnwatchedMovies(@Param("name") String name);

    @Query(" match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) and r2.stars > 3 "
            + " return m2 as movie, r2.stars as stars ")
    List<UserQueryResult> findUnwatchedGoodMovies(@Param("name") String name);

}


Query result bean 定義如下:
package neo4j.springdata.vo.result;

import java.util.List;

import org.springframework.data.neo4j.annotation.QueryResult;

import lombok.Data;
import lombok.ToString;
import neo4j.springdata.vo.Movie;
import neo4j.springdata.vo.User;

@QueryResult
@Data
@ToString
public class UserQueryResult {

    private User friends;
    
    private User user;
    private List<String> movies;
    
    private Movie movie;
    private Integer stars;
    
}


Service class 定義如下:
package neo4j.springdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import neo4j.springdata.repository.UserRepository;
import neo4j.springdata.vo.result.UserQueryResult;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepo;
  
    public List<UserQueryResult> findFriends(String name) {
        return userRepo.findFriends(name);
    }

    public List<UserQueryResult> findRecommendedMovies(String name) {
        return userRepo.findRecommendedMovies(name);
    }

    public List<UserQueryResult> findUnwatchedMovies(String name) {
        return userRepo.findUnwatchedMovies(name);
    }

    public List<UserQueryResult> findUnwatchedGoodMovies(String name) {
        return userRepo.findUnwatchedGoodMovies(name);
    }
}


Client code:
package neo4j.springdata;

import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

import com.google.common.base.Joiner;

import lombok.extern.slf4j.Slf4j;
import neo4j.springdata.service.UserService;
import neo4j.springdata.vo.Movie;
import neo4j.springdata.vo.result.UserQueryResult;

@SpringBootApplication
@EnableNeo4jRepositories
@Slf4j
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    @Bean
    CommandLineRunner queryTest(UserService service) {
        return args -> {
            String name = "John Johnson";
            
            log.info("找出 John Johnson 的朋友:");
            List<UserQueryResult> friends = service.findFriends(name);
            friends.forEach(f->log.info(f.toString()));
            
            log.info("找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影");
            List<UserQueryResult> recommendedMovies = service.findRecommendedMovies(name);
            recommendedMovies.forEach(r -> {
                String uName = r.getUser().getName();
                List<String> movies = r.getMovies();
                log.info("user name = " + uName + ", watched " + Joiner.on(",").join(movies));
            });
            
            log.info("找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影");
            List<UserQueryResult> unwatchedMovies = service.findUnwatchedMovies(name);
            unwatchedMovies.forEach(u -> {
                Movie movie = u.getMovie();
                Integer stars = u.getStars();
                log.info("movie name = " + movie.getName() + ", stars = " + stars);
            });
            
            log.info("找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星");
            List<UserQueryResult> unwatchedGoodMovies = service.findUnwatchedGoodMovies(name);
            unwatchedGoodMovies.forEach(u -> {
                Movie movie = u.getMovie();
                Integer stars = u.getStars();
                log.info("Good movie name = " + movie.getName() + ", stars = " + stars);
            });
        };
    }

}






No comments: