spring - Is it possible to dynamically construct a neo4j cypher query using the GraphRepository pattern -


context : developing java spring boot system backed neo4j database. access database using "classrepo extends graphrepository" structure. writing queries simple case of hard coding in precise query , replacing specified part of supplied parameter (in case coursename).

@query("match (node:course) node.name = {coursename}  return node limit 1") course findbyname(@param("coursename") string name); 

this has worked fine me, allowing me return one, or many results without issue. however, project has developed offer large list of options search (faceted search, think amazon product filters). seems silly write static cipher query each , every permutation of chosen, or not chosen filtering options.

my solution (attempt) pass in parts of query parameters, in essence making string query builder :

@query("match (course:course) -[r]-> (description:courseyeardescription) " +         "with course, count(description) relationcount, collect(description) descriptions " +         "where relationcount > {numberofyears} {returncourse}") iterable<course> findcoursewithnumberofyears(         @param("numberofyears") int numberofyears,         @param("returncourse") string returncourse ); 

where "returncourse" string value "return course". know fact "return course" entered statically in query string works. i've removed , passed string value in parameter see if can generate same query , run @ run time.

this had no real success, returning me error page , printing out following stack:http://pastebin.com/j9vbfpxw

question: there way append/insert strings cypher query strings used graphrepository, query can altered dynamically, ie add clause end of match query @ run time.

no, there isn't. sdn/neo4j ogm not modify custom queries @ run time , merely hands them off executed cypher engine. hence, things can parameterize cypher allow (http://neo4j.com/docs/stable/cypher-parameters.html).

agree not make sense define multiple @query statements each variation, can build query string , use neo4joperations.query* methods accept dynamically generated cypher queries , map of parameters (again, parameters valid in cypher statement).

hope helps.


Comments