包含RegEx的Neo4j自定义Cypher @Query不返回任何结果

时间:2018-01-12 10:17:00

标签: spring neo4j cypher spring-data-neo4j

我在Neo4jRepository中有以下自定义查询(我正在使用带有Spring Data Neo4j的Spring Boot):

@Query("MATCH (n:Component) WHERE n.path =~ '/path/{0}/.*/{1}' RETURN n")
Component findByTenantAndName(String tenant, String name);

当我在应用程序中运行此查询时,它返回null并带有以下日志语句:

2018-01-12 10:58:27.744  INFO 74294 --- [nio-8080-exec-1] o.n.o.drivers.bolt.request.BoltRequest   : Request: MATCH (n:Component) WHERE n.path =~ '/path/{0}/.*/{1}' RETURN n with params {0=foo, 1=baz}
2018-01-12 10:58:27.763  WARN 74294 --- [nio-8080-exec-1] d.c.a.service.MyService  : Could not find parent component named 'baz' for tenant 'foo'

但是,如果我在http://localhost:7474转到Cypher后端并运行查询:

MATCH (n:Component) WHERE n.path =~ '/path/foo/.*/baz' RETURN n

我只用路径Component得到一个/path/foo/bar/baz结果。我的自定义查询有什么问题?

1 个答案:

答案 0 :(得分:2)

@Query("MATCH (n:Component) WHERE n.path =~ {0} RETURN n")
Component findByTenantAndName(String query);

从你称之为的服务。像这样传递整个正则表达式

String query = "/path/<tenant>/.*/<name>"; // replace tenant and name with actual variables.
findByTenantAndName(query)