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

半世苍凉 提交于 2019-12-06 02:12:01

问题


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.


回答1:


No, there isn't. SDN/Neo4j OGM will not modify custom queries at run time and merely hands them off to be executed by the Cypher engine. Hence, the only things you can parameterize are those that Cypher will allow you to (http://neo4j.com/docs/stable/cypher-parameters.html).

Agree that it does not make sense to define multiple @Query statements for each variation, but what you can do is build your query as a string and use the Neo4jOperations.query* methods that will accept your dynamically generated Cypher queries and a map of parameters (again, parameters that are valid in a Cypher statement).

Hope that helps.



来源:https://stackoverflow.com/questions/34979333/is-it-possible-to-dynamically-construct-a-neo4j-cypher-query-using-the-graphrepo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!