https://github.com/mr-smithers-excellent/elasticsearch-spring-boot-geosearch
Spring Boot RESTful API with geosearch capabilities via Elasticsearch with support for Docker & AWS
https://github.com/mr-smithers-excellent/elasticsearch-spring-boot-geosearch
aws-elasticsearch docker elasticsearch jenkins mysql rest-api spring-boot spring-data-elasticsearch spring-data-jpa spring-data-rest
Last synced: 2 months ago
JSON representation
Spring Boot RESTful API with geosearch capabilities via Elasticsearch with support for Docker & AWS
- Host: GitHub
- URL: https://github.com/mr-smithers-excellent/elasticsearch-spring-boot-geosearch
- Owner: mr-smithers-excellent
- License: apache-2.0
- Created: 2017-07-16T22:48:03.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-08T00:11:06.000Z (almost 8 years ago)
- Last Synced: 2025-03-22T22:05:24.661Z (2 months ago)
- Topics: aws-elasticsearch, docker, elasticsearch, jenkins, mysql, rest-api, spring-boot, spring-data-elasticsearch, spring-data-jpa, spring-data-rest
- Language: Java
- Homepage:
- Size: 731 KB
- Stars: 15
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Elasticsearch Spring Boot Geosearch
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
## Geosearch OptionsThis project demonstrates two ways in which you can utilize Elasticsearch's geospatial search capabilities via the Spring Data Elasticsearch library.
1. **ElasticsearchRepository method** - implementation automatically taken care of by Spring Data Elasticsearch via naming convention `findByLocationNear()`
```java
@RepositoryRestResource(path = "/starbucks-locations", collectionResourceRel = "/starbucks-locations")
public interface StarbucksSearchRepository extends ElasticsearchRepository, StarbucksSearchRepositoryCustom {Page findByLocationNear(@Param("location") Point point, @Param("distance") Distance distance, Pageable pageable);
}
```
2. **Custom Repository method** - implementation defined in `StarbucksSearchRepositoryImpl`
```java
@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 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
);
}}
```
## Technologies* [Spring Boot](https://projects.spring.io/spring-boot/)
* [Spring Data Elasticsearch](https://projects.spring.io/spring-data-elasticsearch/)
* [Spring Data JPA](https://projects.spring.io/spring-data-jpa/)
* [Spring Data REST](https://projects.spring.io/spring-data-rest/)
* [Spring Data Jest](https://github.com/VanRoy/spring-data-jest)
* [Docker](https://www.docker.com/)
* [AWS Elasticsearch Service](https://aws.amazon.com/elasticsearch-service/)
* [AWS Relational Database Service](https://aws.amazon.com/rds/)## Configuration Options
| Spring Profile | Database | Elasticsearch |
|:--------------:|:--------:|:-------------:|
| default | in-memory H2 | in-memory |
| docker | [latest MySQL image](https://hub.docker.com/r/library/mysql/) | [Elasticsearch 2.4.5-alpine image](https://hub.docker.com/r/library/elasticsearch/) |
| aws | AWS RDS | AWS Elasticsearch Service |## Running Locally
### Prerequisites
* Java 8
* Maven### Steps
1. Set the Spring profile to `default` in `application.yml` (only if previously changed)
```YAML
spring.profiles.active: default
```2. Run the Java project using `mvn spring-boot:run` on the command line or using your favorite IDE
## Running on Docker
### Prerequisites
* [Docker Engine](https://docs.docker.com/engine/installation/)### Steps
1. Set the Spring profile to `docker` in `application.yml`
```YAML
spring.profiles.active: docker
```2. From the root directory of this project, run `docker-compose up` on the command line
3. Run the Java project using `mvn spring-boot:run` on the command line or using your favorite IDE
## Running on AWS Elasticsearch Service & RDS
### Prerequisites
* AWS Elasticsearch Service cluster
* RDS instance up and running (or any other MySQL database)### Steps
1. Set the Spring profile to `aws` in `application.yml`
```YAML
spring.profiles.active: aws
```
2. Modify the `application.yml` file with your Elasticsearch & RDS settings.
```YAML
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]
```3. Run the Java project using `mvn spring-boot:run` on the command line or using your favorite IDE
## Running the Tests
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
```## Acknowledgements
Starbucks location data provided by [Socrata](https://opendata.socrata.com/Business/All-Starbucks-Locations-in-the-US/txu4-fsic)