Search code examples
javaspringelasticsearchspring-dataspring-data-elasticsearch

Custom GeoPoint object in Spring Data Elasticsearch


Using spring boot 2.3.4.

Is there a way to use a custom GeoPoint object instead of org.springframework.data.elasticsearch.core.geo.GeoPoint ?

If so, how could the mapping be overridden ? putMapping is deprecated.

@Document(indexName="myIndex")
public class MyDocument{
    @GeoPointField
    private MyPositionDocument position;
    // currently this only works with
    // private GeoPoint position;

Solution

  • You already have the first part, annotating the property with @GeoPointField. This ensures that the correct index mappings will be written so that the type of position will be geo_point.

    The next thing is that you will need to create custom converters to convert from a MyPositionDocument to a type that is supported by Elasticsearch (https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html#geo-point) and one converter for the other direction.

    As for registering custom converters, see https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.conversions