From f220075b72557244dd8d683fa0666d2ff3e03de9 Mon Sep 17 00:00:00 2001 From: fmarchand Date: Wed, 6 Nov 2013 14:52:38 +0100 Subject: [PATCH 1/2] Update README.md --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index d55ea5b1c..acb52fc1c 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,56 @@ Indexing multiple Document(bulk index) using Repository repository.save(sampleEntities); ``` +### Geo indexing and request + +You can make request using geo_distance filter. This can be done using GeoPoint object. + +First, here is a sample of an entity with a GeoPoint field + +```java +@Document(indexName = "test-geo-index", type = "geo-class-point-type") +public class AuthorMarkerEntity { + + @Id + private String id; + private String name; + + private GeoPoint location; + + private AuthorMarkerEntity(){ + } + + public AuthorMarkerEntity(String id){ + this.id = id; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public GeoPoint getLocation() { + return location; + } + + public void setLocation(GeoPoint location) { + this.location = location; + } +} +``` + + ### XML Namespace You can set up repository scanning via xml configuration, which will happily create your repositories. From 888888866d4b810473b5816b5f8b9c842714797f Mon Sep 17 00:00:00 2001 From: fmarchand Date: Sun, 17 Nov 2013 13:36:56 +0100 Subject: [PATCH 2/2] Update README.md How to use "Geo-entities" (indexing, queries) --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index acb52fc1c..bd6e83654 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ Indexing multiple Document(bulk index) using Repository You can make request using geo_distance filter. This can be done using GeoPoint object. -First, here is a sample of an entity with a GeoPoint field +First, here is a sample of an entity with a GeoPoint field (location) ```java @Document(indexName = "test-geo-index", type = "geo-class-point-type") @@ -223,6 +223,45 @@ public class AuthorMarkerEntity { } ``` +Indexing your entity with a geo-point : +```java +elasticsearchTemplate.createIndex(AuthorMarkerEntity.class); +elasticsearchTemplate.refresh(AuthorMarkerEntity.class, true); +elasticsearchTemplate.putMapping(AuthorMarkerEntity.class); + +List indexQueries = new ArrayList(); +indexQueries.add(new AuthorMarkerEntityBuilder("1").name("Franck Marchand").location(45.7806d, 3.0875d).buildIndex()); +indexQueries.add(new AuthorMarkerEntityBuilder("2").name("Mohsin Husen").location(51.5171d, 0.1062d).buildIndex()); +indexQueries.add(new AuthorMarkerEntityBuilder("3").name("Rizwan Idrees").location(51.5171d, 0.1062d).buildIndex()); +elasticsearchTemplate.bulkIndex(indexQueries); +``` + +For your information : +- Clermont-Ferrand : 45.7806, 3.0875 +- London : 51.5171, 0.1062 + +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 : + +```java +CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery( + new Criteria("location").within(new GeoPoint(45.7806d, 3.0875d), "20km")); +List geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerEntity.class); +``` + +This example should only return one author named "Franck Marchand". + +You can even combine with other criteria (e.g. author name) : + +Here, we're looking for authors located within 20 kilometers around London AND named "Mohsin Husen" : + +```java +CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery( + new Criteria("name").is("Mohsin Husen").and("location").within(new GeoPoint(51.5171d, 0.1062d), "20km")); +List geoAuthorsForGeoCriteria2 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery2, AuthorMarkerEntity.class); +``` + +This example should only return one author named "Mohsin Husen". + ### XML Namespace