0

Based on Spring Data Redis guide, and having the following defined:

Model

@RedisHash("positions")
public class Position {

    @Id
    private String id;

    @GeoIndexed
    private Point coordinates;

    //...
}

Connection

@Configuration
public class RedisConfig {
    @Bean
    public RedisConnectionFactory lettuceConnectionFactory() {
        return new LettuceConnectionFactory("localhost", 6379);
    }

    @Bean
    public RedisTemplate<String, Position> redisTemplate() {

        final RedisTemplate<String, Position> template = new RedisTemplate<String, Position>();
        template.setConnectionFactory(lettuceConnectionFactory());
        return template;
    }
}

I want to query for positions by its coordinates, that is why I defined the following repository:

Repository

public interface PositionRepository extends CrudRepository<Position, String> {
    List<Position> findByCoordinates(Point point);
    //...
}

but calling that method results in org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.geo.Point] to type [byte[]]

How can I define/implement that query?

y.luis.rojo
  • 1,794
  • 4
  • 22
  • 41

1 Answers1

1

TL;DR;

You need to use the Near keyword to indicate it's a geo query.

Explanation

Spring Data derives from query methods the property to match and its actual match mode. There are several match modes and the default is exact (simple property) matching by comparing the criteria against the stored value.

Change your code to

public interface PositionRepository extends CrudRepository<Position, String> {
    List<Position> findByCoordinatesNear(Point point);
}

to execute a Geo query.

It's not really relevant how you annotated your object as you're specifying query methods from the repository perspective.

See also:

mp911de
  • 17,546
  • 2
  • 55
  • 95
  • The code change you are proposing results in `org.springframework.dao.InvalidDataAccessApiUsageException: Expected to find distance value for geo query. Are you missing a parameter?` What I am trying to achieve, I would not call it a geoquery, but a "classic" query whether those coordinates exists on the db. Is that possible? – y.luis.rojo Jan 11 '18 at 15:53
  • Ok, got it. Then you must store coordinates in separate fields and index both. Keep in mind that Redis has no notion of data types of the actual payload which compares your points (double/float values) as strings. – mp911de Jan 12 '18 at 08:43
  • Thanks again for your comment. For now, I decided to encode the latitude and longitude and use it as key. I do not know if it is the best solution (or drawbacks), but it works since I can just query by the encoded id. – y.luis.rojo Jan 12 '18 at 13:47