Search code examples
javaspringneo4jspring-dataspring-data-neo4j

Spring Neo4j Pass Node Property as Parameter


I am trying to pass a property as a parameter.

I have tried this:

String get_interest="MATCH(user:User{id:{id}})-[watched:WATCHED]->(movie:Movie{title:{title}}) " +
        "MATCH(movie)-[:BELONGS_TO]->(category:Category) " +
        "MATCH(category)<-[:BELONGS_TO]-(similarMovie:Movie) " +
        "WHERE NOT EXISTS((user) -[:WATCHED]->(similarMovie))" +
        "RETURN similarMovie Limit 20";

But it fails with this error:

"error": "Internal Server Error",
    "message": "Cypher execution failed with code 'Neo.ClientError.Statement.SyntaxError': Invalid input '{': expected \"+\" or \"-\" (line 1, column 24 (offset: 23))

I have tried this and it worked:

String get_interest="MATCH(user:User{id:\"02331\"})-[watched:WATCHED]->(movie:Movie{title:\"The Mask\"}) " +
        "MATCH(movie)-[:BELONGS_TO]->(category:Category) " +
        "MATCH(category)<-[:BELONGS_TO]-(similarMovie:Movie) " +
        "WHERE NOT EXISTS((user) -[:WATCHED]->(similarMovie))" +
        "RETURN similarMovie Limit 20";

But I want to pass other User ids and titles.

This is my Repository

@Query(get_interest)
Collection<Movie> getMovieByInterest(@Param("id") String id,
                                         @Param("title") String title);

Solution

  • Params are bound as variables. Variables are interpolated with dollar:

    "MATCH(user:User{id:$id})-[watched:WATCHED]->(movie:Movie{title:$title})"