CONTEXT : I am developing a java Spring Boot system backed by a neo4j database. I access the database using the "ClassRepo extends GraphRepository" structure. Writing queries is a simple case of hard coding in my precise query and replacing a specified part of it with a supplied parameter (in this case courseName).
@Query("MATCH (node:Course) WHERE node.name = {courseName} RETURN node LIMIT 1")
Course findByName(@Param("courseName") String name);
This has all worked fine for me, allowing me to return one, or many results without issue. However, as my project has developed I now offer a large list of options to search by (faceted search, think amazon product filters). It seems silly to write a static cipher query for each and every permutation of chosen, or not chosen filtering options.
My solution (attempt) was to pass in parts of the query as parameters, in essence making a string query builder :
@Query("MATCH (course:Course) -[r]-> (description:CourseYearDescription) " +
"WITH course, count(description) as relationCount, collect(description) as descriptions " +
"WHERE relationCount > {numberOfYears} {returnCourse}")
Iterable<Course> findCourseWithNumberOfYears(
@Param("numberOfYears") int numberOfYears,
@Param("returnCourse") String returnCourse
);
Where "returnCourse" is a string with the value "RETURN course". I know for a fact that "RETURN course" entered statically in the query string works. I've just removed it and passed the string value in as a parameter to see if it can generate the same query and run it at run time.
This had no real success, returning me to an error page and printing out the following stack:http://pastebin.com/J9VBfpxw
QUESTION: Is there a way to append/insert strings into a cypher query strings used by a GraphRepository, so that the query can be altered dynamically, ie add a where clause to the end of a match query at run time.