Spring Data Elasticsearch and GeoPoints
Tags: elasticsearch, springframework
Published: 2019-08-21 19:17:36

---
title: Spring Data Elasticsearch and GeoPoints
published: true
tags: elasticsearch,spring framework
canonical_url: https://blog.joshmlwood.com/spring-data-elasticsearch-and-geopoints/
---

 ![Spring Data Elasticsearch and GeoPoints](https://images.unsplash.com/photo-1524146128017-b9dd0bfd2778?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Some backstory... I was working on a Java app with an Angular 1.x frontend. The base project was generated by [JHipster](http://jhipster.github.io/) and uses PostgreSQL for the DB and Spring Boot.

The application relies heavily on being able to return a list of items that are "near" the provided location from the client. So really just a geospatial search. To accomplish this I decided to use Elasticsearch's built in geo functions. I figured the easiest way to do so would be to use my JPA entity that is annotated as a Hibernate entity could double for my search engine entity as long as I used a string to represent my geopoint. It was pretty simple to set that up, just concatenate latitude, a comma, and longitude, or the reverse depending on use cases or to conform to Elasticsearch's requirements. Now we can easily store it in the database without a serializer and deserializer, and it should work out of the box with Elasticsearch.

I wrote a couple tests with dummy data to make sure that I can do geo based searches and return exactly what I expect. I.E. I don't return a lat/lon that's in New York when I search for results within 1 kilometer of Chicago Loop. When I ran the tests, all I saw was red. I was very disheartened by this. I followed exactly what the Spring Data Elasticsearch developers did with their tests. I thought I had it set up correctly. I made sure my geopoint string with annotated with `@GeoPointField`, and that my entity was annotated as a `@Document` for ElasticSearch. But alas, no matter what I tried Elasticsearch could not process my string as a geopoint.

Then I took another look at the documentation and the tests on github for Spring Data Elasticsearch. You can look at the [LocationMarkerEntity.java](https://github.com/spring-projects/spring-data-elasticsearch/blob/b2f0300856cb3dd9bfeade3e1db26da96bc2d88a/src/test/java/org/springframework/data/elasticsearch/core/geo/LocationMarkerEntity.java) entity for their tests as an example. In their tests they are able to return items using the geo spacial queries for the geopoint String fields. The only difference between my entity and their entity was some additonal options on the `@Document` annotation. I thought that since I was using the `@GeoPointField` annotation on my string, it would work without a hitch, however that's not the case. As you can see below, the source for the Spring Data Elasticsearch also included this `type = "geo-annotation-point-type"` option in the `@Document` annotation.

```
package org.springframework.data.elasticsearch.core.geo;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.GeoPointField;

@Document(indexName = "test-geo-index", type = "geo-annotation-point-type", shards = 1, replicas = 0, refreshInterval = "-1")
public class LocationMarkerEntity {
	// some code omitted for berevity
	@GeoPointField
	private String locationAsString;

	@GeoPointField
	private double[] locationAsArray;
	// omitted
}
```

I searched the [Spring Data Elasticsearch docs](http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/) but I didn't find a reference to this type parameter. I did a cursory search of the Elasticsearch docs, but didn't turn anything up there. I also looked through the [Spring Data Elasticsearch source](https://github.com/spring-projects/spring-data-elasticsearch) to see if this parameter was defined in the `@Document` annotation but I didn't find it. I likely missed it in the docs or in the source, but it lead to a frustrating few days as I spent time trying to find why my geo indexing wasn't working correctly.

Hopefully this will help anybody else having the same issue with the Spring Data Elasticsearch framework.