Joe Pisquali
Joe Pisquali

Reputation: 3

Spring Neo4j 4 (SDN) Wildcard Query methods not working

Trying out SDN 4 and finding that although certain queries work in the cypher browser, they don't seem to work in my repository. For example, when I enter the query:

MATCH (p:Publication) WHERE p.name =~'(?i)e.*' RETURN p;

in the cypher browser, it returns the expected results. However, with my repository defined as:

public interface PublicationRepo extends GraphRepository<Publication> {
    Publication findByName(String name);
    @Query(value="MATCH (p:Publication) WHERE p.name=~'(?i){0}.*' RETURN p")
    Iterable<Publication> findByNameLikeIgnoreCase(String name);
}

it returns zero results.

The project setup is functioning properly as I can get data from other custom query methods, but the wildcard match isn't working

Additionally, I find it odd that the standard Spring Data JPA query methods also do not work (e.g. 'findByNameContaining', etc). Has anyone else run into this or am I doing something wrong. All the examples I've seen are very basic.

Upvotes: 0

Views: 176

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41676

It is not a spring data neo4j issue. You use the parameter within a string, where it is not evaluated.

Change it to this, which concatenates the string for the regular expression, using the parameter.

MATCH (p:Publication) WHERE p.name=~ ('(?i)' + {0} + '.*') RETURN p

public interface PublicationRepo extends GraphRepository<Publication> {
    Publication findByName(String name);
    @Query("MATCH (p:Publication) WHERE p.name=~ ('(?i)'+{0}+'.*') RETURN p")
    Iterable<Publication> findByNameLikeIgnoreCase(String name);
}

Upvotes: 1

Joe Pisquali
Joe Pisquali

Reputation: 3

Not sure if there is a better way to solve this issue, but essentially I concatenated the wildcard tokens with the search string before passing it to the finder method. Code as such:

public interface PublicationRepo extends GraphRepository<Publication> {
    Publication findByName(String name);
    @Query(value="MATCH (p:Publication)-[r:HAS_PUBLICATION]-(i:Issue) WHERE p.name=~{0} RETURN p, r, i")
    Iterable<Publication> findByNameLikeIgnoreCase(String name);
}

This now is working, but I'm not real fond of the caller needing to massage the query parameter. C'est la vie.

Upvotes: 0

Related Questions