rodrigocoelho rodrigocoelho - 26 days ago 17
Java Question

Spring data neo4j LIKE query not working

I'm new on neo4j spring data, and the query i'm trying to do its not working on my spring boot application, but working on the neo4j web interface.

I have 3 nodes on the database: "oracle", "java" and "cloud"
The query i'm trying to run:

MATCH(interest:Interest) WHERE interest.name =~ '(?i).*cl.*' RETURN interest


So, using the neo4j web interface, if i put "cl" it will return "oracle" and "cloud", which is correct.

Using spring data, i get all 3 results, which is wrong.

My repository interface:

@Query("MATCH(interest:Interest) WHERE interest.name =~ '(?i).*{0}.*' RETURN interest")
Set<Interest> getKindOf(String value);


My rest controller:

@ResponseBody
public Collection<Interest> getPersonByName(@PathVariable String name) {
Set<Interest> interests = interestRepository.getKindOf(name);
return interests;
}


Checking Spring boot logs i can see the query:

INFO 12716 --- [nio-2016-exec-1] o.n.o.drivers.http.request.HttpRequest : Thread: 22, url: http://localhost:7474/db/data/transaction/commit, request: {"statements":[{"statement":"MATCH(interest:Interest) WHERE interest.name =~ '(?i).*{0}.*' RETURN interest","parameters":{"0":"cl"},"resultDataContents":["graph"],"includeStats":false}]}


So, the question is: Why i get 2 results using neo4j web interface (OK) and 3 results using spring data (NOK)?

Thanks for the help!

Rodrigo

Answer

The complete regex should be passed as argument (aka cypher query parameter)

@Query("MATCH(interest:Interest) WHERE interest.name =~ {0} RETURN interest")
Set<Interest> getKindOf(String value);

--

@ResponseBody
public Collection<Interest> getPersonByName(@PathVariable String name) {
    String regex = "(?i).*" + name + ".*";
    Set<Interest> interests = interestRepository.getKindOf(regex);
    return interests;
}

Note this has to do with the cypher parameters handling, this is not specific to sdn

Comments