DATAES-12 Merge Geo Location from branch 'master' of https://github.com/fmarchand/spring-data-elasticsearch

This commit is contained in:
Artur Konczak
2013-06-13 17:31:14 +01:00
parent aa6a5e3753
commit 74a4035408
15 changed files with 796 additions and 253 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.geo.GeoLocation;
/**
* @author Franck Marchand
*/
@Document(indexName = "test-geo-index", type = "test-geo-type")
public class GeoAuthor {
@Id
private String id;
private String name;
private GeoLocation location;
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 GeoLocation getLocation() {
return location;
}
public void setLocation(GeoLocation location) {
this.location = location;
}
}

View File

@@ -25,11 +25,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.GeoAuthor;
import org.springframework.data.elasticsearch.SampleEntity;
import org.springframework.data.elasticsearch.SampleMappingEntity;
import org.springframework.data.elasticsearch.core.geo.GeoBBox;
import org.springframework.data.elasticsearch.core.geo.GeoLocation;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -42,12 +44,14 @@ import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Franck Marchand
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@@ -56,12 +60,18 @@ public class ElasticsearchTemplateTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
@Before
public void before(){
elasticsearchTemplate.deleteIndex(SampleEntity.class);
elasticsearchTemplate.createIndex(SampleEntity.class);
elasticsearchTemplate.refresh(SampleEntity.class, true);
}
elasticsearchTemplate.createIndex(SampleEntity.class);
elasticsearchTemplate.refresh(SampleEntity.class, true);
elasticsearchTemplate.deleteIndex(GeoAuthor.class);
elasticsearchTemplate.createIndex(GeoAuthor.class);
elasticsearchTemplate.refresh(GeoAuthor.class, true);
elasticsearchTemplate.putMapping(GeoAuthor.class);
}
@Test
public void shouldReturnCountForGivenSearchQuery() {
@@ -716,4 +726,85 @@ public class ElasticsearchTemplateTests {
assertThat(elasticsearchTemplate.indexExists(clazz), is(false));
}
@Test
public void shouldPutMappingForGivenEntityWithGeoLocation()throws Exception{
//given
Class entity = GeoAuthor.class;
elasticsearchTemplate.createIndex(entity);
//when
assertThat(elasticsearchTemplate.putMapping(entity) , is(true)) ;
}
@Test
public void shouldReturnListForGivenCriteriaWithGeoLocation(){
//given
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
//first document
String documentId = randomNumeric(5);
GeoAuthor geoAuthor1 = new GeoAuthor();
geoAuthor1.setId(documentId);
geoAuthor1.setName("Franck Marchand");
geoAuthor1.setLocation(new GeoLocation(45.7806d, 3.0875d)); // Clermont-Ferrand
IndexQuery indexQuery1 = new IndexQuery();
indexQuery1.setId(documentId);
indexQuery1.setObject(geoAuthor1);
indexQueries.add(indexQuery1);
//second document
String documentId2 = randomNumeric(5);
GeoAuthor geoAuthor2 = new GeoAuthor();
geoAuthor2.setId(documentId2);
geoAuthor2.setName("Mohsin Husen");
geoAuthor2.setLocation(new GeoLocation(51.5171d, 0.1062d)); // London
IndexQuery indexQuery2 = new IndexQuery();
indexQuery2.setId(documentId2);
indexQuery2.setObject(geoAuthor2);
indexQueries.add(indexQuery2);
//third document
String documentId3 = randomNumeric(5);
GeoAuthor geoAuthor3 = new GeoAuthor();
geoAuthor3.setId(documentId3);
geoAuthor3.setName("Rizwan Idrees");
geoAuthor3.setLocation(new GeoLocation(51.5171d, 0.1062d)); // London
IndexQuery indexQuery3 = new IndexQuery();
indexQuery3.setId(documentId3);
indexQuery3.setObject(geoAuthor3);
indexQueries.add(indexQuery3);
//when
elasticsearchTemplate.bulkIndex(indexQueries);
elasticsearchTemplate.refresh(GeoAuthor.class, true);
//when
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
new Criteria("location").within(new GeoLocation(45.7806d, 3.0875d), "20km"));
List<GeoAuthor> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery,GeoAuthor.class);
//then
assertThat(geoAuthorsForGeoCriteria.size(),is(1));
assertEquals("Franck Marchand", geoAuthorsForGeoCriteria.get(0).getName());
// query/filter geo distance mixed query
CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery(
new Criteria("name").is("Mohsin Husen").and("location").within(new GeoLocation(51.5171d, 0.1062d), "20km"));
List<GeoAuthor> geoAuthorsForGeoCriteria2 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery2,GeoAuthor.class);
assertThat(geoAuthorsForGeoCriteria2.size(),is(1));
assertEquals("Mohsin Husen", geoAuthorsForGeoCriteria2.get(0).getName());
// bbox query
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
new Criteria("location").bbox(
new GeoBBox(new GeoLocation(53.5171d, 0),
new GeoLocation(49.5171d, 0.2062d))));
List<GeoAuthor> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3,GeoAuthor.class);
assertThat(geoAuthorsForGeoCriteria3.size(),is(2));
assertThat(geoAuthorsForGeoCriteria3, containsInAnyOrder(hasProperty("name", equalTo("Mohsin Husen")), hasProperty("name",equalTo("Rizwan Idrees"))));
}
}