Cypher Search Query Fuzzy query

235 views Asked by At

I want to use cypher to search,

  1. I have four movie enerties, forrest, sky, sky1, sky2
  2. I want search sky
  3. I want it returns sky, sky1, sky2

My cypher is

@Query("MATCH (movie:Movie) WHERE movie.title =~ '.*{0}.*' RETURN movie")

or

@Query("MATCH (movie:Movie) WHERE movie.title =~ '(?i).*{0}.*' RETURN movie")

Neither of those works well: it return forrest, sky,sky1, sky2 no matter what I search (forrest or sky). What is wrong? controller

@RequestMapping(value = "/movies", method = RequestMethod.GET, headers = "Accept=text/html")
public String findMovies(Model model, @RequestParam("q") String query) {
    if (query != null && !query.isEmpty()) {

        List<Movie> movies = movieRepository.findByTitleLike("(?i).*sky.*");

        model.addAttribute("movies", IteratorUtil.asCollection(movies));

    } else {
        model.addAttribute("movies", Collections.emptyList());
    }
    model.addAttribute("query", query);

    return "movies/list";
}
1

There are 1 answers

2
JohnMark13 On

The last time I was doing regular expression matching was in a previous version of Neo4j, at that time you could not construct the regular expression using a parameter in this way. The {0} does not get converted. Instead you should change your query to:

@Query("MATCH (movie:Movie) WHERE movie.title =~ {0} RETURN movie")
List<Movie> findByTitleLike(String like)

And call it with:

myMovieRepository.findByTitleLike("(?i).*sky.*")