RESTful API that allows a user to find Starbucks locations based on one of the following criteria:
- Location (latitude, longitude & distance)
- City
- State
- Zip code
This project demonstrates two ways in which you can utilize Elasticsearch's geospatial search capabilities via the Spring Data Elasticsearch library.
- ElasticsearchRepository method - implementation automatically taken care of by Spring Data Elasticsearch via naming convention
findByLocationNear()
@RepositoryRestResource(path = "/starbucks-locations", collectionResourceRel = "/starbucks-locations")
public interface StarbucksSearchRepository extends ElasticsearchRepository<Starbucks, Long>, StarbucksSearchRepositoryCustom {
Page<Starbucks> findByLocationNear(@Param("location") Point point, @Param("distance") Distance distance, Pageable pageable);
}
- Custom Repository method - implementation defined in
StarbucksSearchRepositoryImpl
@Repository
public class StarbucksSearchRepositoryImpl implements StarbucksSearchRepositoryCustom {
private final JestClient jestClient;
private final JestElasticsearchTemplate elasticsearchTemplate;
public StarbucksSearchRepositoryImpl(JestClient jestClient) {
this.jestClient = jestClient;
this.elasticsearchTemplate = new JestElasticsearchTemplate(this.jestClient);
}
@Override
public Page<Starbucks> findByLocationWithin(Point point, Distance distance, Pageable pageable) {
return elasticsearchTemplate.queryForPage(getGeoQuery(point, distance, pageable), Starbucks.class);
}
private CriteriaQuery getGeoQuery(Point point, Distance distance, Pageable pageable) {
return new CriteriaQuery(
new Criteria("location").within(point, distance),
pageable
);
}
}
- Spring Boot
- Spring Data Elasticsearch
- Spring Data JPA
- Spring Data REST
- Spring Data Jest
- Docker
- AWS Elasticsearch Service
- AWS Relational Database Service
Spring Profile | Database | Elasticsearch |
---|---|---|
default | in-memory H2 | in-memory |
docker | latest MySQL image | Elasticsearch 2.4.5-alpine image |
aws | AWS RDS | AWS Elasticsearch Service |
- Java 8
- Maven
- Set the Spring profile to
default
inapplication.yml
(only if previously changed)
spring.profiles.active: default
- Run the Java project using
mvn spring-boot:run
on the command line or using your favorite IDE
- Set the Spring profile to
docker
inapplication.yml
spring.profiles.active: docker
-
From the root directory of this project, run
docker-compose up
on the command line -
Run the Java project using
mvn spring-boot:run
on the command line or using your favorite IDE
- AWS Elasticsearch Service cluster
- RDS instance up and running (or any other MySQL database)
- Set the Spring profile to
aws
inapplication.yml
spring.profiles.active: aws
- Modify the
application.yml
file with your Elasticsearch & RDS settings.
spring:
profiles: aws
data.jest:
uri: https://[AWS_ELASTICSEARCH_URI] # URI for AWS Elasticsearch index
aws-region: [AWS_REGION_NAME]
datasource: # RDS settings
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://[AWS_DB_HOST]:3306/starbucks
username: [DB_USERNAME]
password: [DB_PASSWORD]
- Run the Java project using
mvn spring-boot:run
on the command line or using your favorite IDE
There are a complete set of unit tests covering core search capabilities as well as data population/synchronization from a MySQL database. To run the tests from the command line:
mvn test
Starbucks location data provided by Socrata