Skip to content

Commit b31a9d1

Browse files
committed
Merge pull request spring-projects#28 from fmarchand/master
Documentation (README.md) for Geo Location / Filter Feature
2 parents 56d4079 + 8888888 commit b31a9d1

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,95 @@ Indexing multiple Document(bulk index) using Repository
166166
repository.save(sampleEntities);
167167
```
168168

169+
### Geo indexing and request
170+
171+
You can make request using geo_distance filter. This can be done using GeoPoint object.
172+
173+
First, here is a sample of an entity with a GeoPoint field (location)
174+
175+
```java
176+
@Document(indexName = "test-geo-index", type = "geo-class-point-type")
177+
public class AuthorMarkerEntity {
178+
179+
@Id
180+
private String id;
181+
private String name;
182+
183+
private GeoPoint location;
184+
185+
private AuthorMarkerEntity(){
186+
}
187+
188+
public AuthorMarkerEntity(String id){
189+
this.id = id;
190+
}
191+
192+
public String getId() {
193+
return id;
194+
}
195+
196+
public void setId(String id) {
197+
this.id = id;
198+
}
199+
200+
public String getName() {
201+
return name;
202+
}
203+
204+
public void setName(String name) {
205+
this.name = name;
206+
}
207+
208+
public GeoPoint getLocation() {
209+
return location;
210+
}
211+
212+
public void setLocation(GeoPoint location) {
213+
this.location = location;
214+
}
215+
}
216+
```
217+
218+
Indexing your entity with a geo-point :
219+
```java
220+
elasticsearchTemplate.createIndex(AuthorMarkerEntity.class);
221+
elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true);
222+
elasticsearchTemplate.putMapping(AuthorMarkerEntity.class);
223+
224+
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
225+
indexQueries.add(new AuthorMarkerEntityBuilder("1").name("Franck Marchand").location(45.7806d, 3.0875d).buildIndex());
226+
indexQueries.add(new AuthorMarkerEntityBuilder("2").name("Mohsin Husen").location(51.5171d, 0.1062d).buildIndex());
227+
indexQueries.add(new AuthorMarkerEntityBuilder("3").name("Rizwan Idrees").location(51.5171d, 0.1062d).buildIndex());
228+
elasticsearchTemplate.bulkIndex(indexQueries);
229+
```
230+
231+
For your information :
232+
- Clermont-Ferrand : 45.7806, 3.0875
233+
- London : 51.5171, 0.1062
234+
235+
So, if you want to search for authors who are located within 20 kilometers around Clermont-Ferrand, here is the way to build your query :
236+
237+
```java
238+
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
239+
new Criteria("location").within(new GeoPoint(45.7806d, 3.0875d), "20km"));
240+
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerEntity.class);
241+
```
242+
243+
This example should only return one author named "Franck Marchand".
244+
245+
You can even combine with other criteria (e.g. author name) :
246+
247+
Here, we're looking for authors located within 20 kilometers around London AND named "Mohsin Husen" :
248+
249+
```java
250+
CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery(
251+
new Criteria("name").is("Mohsin Husen").and("location").within(new GeoPoint(51.5171d, 0.1062d), "20km"));
252+
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria2 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery2, AuthorMarkerEntity.class);
253+
```
254+
255+
This example should only return one author named "Mohsin Husen".
256+
257+
169258
### XML Namespace
170259

171260
You can set up repository scanning via xml configuration, which will happily create your repositories.

0 commit comments

Comments
 (0)