Browse Source
We now use the $geoWithin operator for geospatial criteria which requires to run on at least MongoDB 2.4. Original pull request: #263.pull/265/head
9 changed files with 411 additions and 234 deletions
@ -0,0 +1,68 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010-2015 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.mongodb.core.geo; |
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.data.domain.Sort.Direction; |
||||||
|
import org.springframework.data.mongodb.core.IndexOperations; |
||||||
|
import org.springframework.data.mongodb.core.Venue; |
||||||
|
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; |
||||||
|
import org.springframework.data.mongodb.core.index.GeospatialIndex; |
||||||
|
import org.springframework.data.mongodb.core.index.IndexField; |
||||||
|
import org.springframework.data.mongodb.core.index.IndexInfo; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Christoph Strobl |
||||||
|
*/ |
||||||
|
public class GeoSpatial2DSphereTests extends AbstractGeoSpatialTests { |
||||||
|
|
||||||
|
/** |
||||||
|
* @see DATAMONGO-360 |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void indexInfoIsCorrect() { |
||||||
|
|
||||||
|
IndexOperations operations = template.indexOps(Venue.class); |
||||||
|
List<IndexInfo> indexInfo = operations.getIndexInfo(); |
||||||
|
|
||||||
|
assertThat(indexInfo.size(), is(2)); |
||||||
|
|
||||||
|
List<IndexField> fields = indexInfo.get(0).getIndexFields(); |
||||||
|
assertThat(fields.size(), is(1)); |
||||||
|
assertThat(fields, hasItem(IndexField.create("_id", Direction.ASC))); |
||||||
|
|
||||||
|
fields = indexInfo.get(1).getIndexFields(); |
||||||
|
assertThat(fields.size(), is(1)); |
||||||
|
assertThat(fields, hasItem(IndexField.geo("location"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void createIndex() { |
||||||
|
template.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location").typed(GeoSpatialIndexType.GEO_2DSPHERE)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void dropIndex() { |
||||||
|
template.indexOps(Venue.class).dropIndex("location_2dsphere"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,82 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2010-2015 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.mongodb.core.geo; |
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
import static org.springframework.data.mongodb.core.query.Criteria.*; |
||||||
|
import static org.springframework.data.mongodb.core.query.Query.*; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.data.domain.Sort.Direction; |
||||||
|
import org.springframework.data.geo.Point; |
||||||
|
import org.springframework.data.mongodb.core.IndexOperations; |
||||||
|
import org.springframework.data.mongodb.core.Venue; |
||||||
|
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; |
||||||
|
import org.springframework.data.mongodb.core.index.GeospatialIndex; |
||||||
|
import org.springframework.data.mongodb.core.index.IndexField; |
||||||
|
import org.springframework.data.mongodb.core.index.IndexInfo; |
||||||
|
|
||||||
|
/** |
||||||
|
* Modified from https://github.com/deftlabs/mongo-java-geospatial-example
|
||||||
|
* |
||||||
|
* @author Mark Pollack |
||||||
|
* @author Oliver Gierke |
||||||
|
* @author Thomas Darimont |
||||||
|
* @author Christoph Strobl |
||||||
|
*/ |
||||||
|
public class GeoSpatial2DTests extends AbstractGeoSpatialTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void nearPoint() { |
||||||
|
Point point = new Point(-73.99171, 40.738868); |
||||||
|
List<Venue> venues = template.find(query(where("location").near(point).maxDistance(0.01)), Venue.class); |
||||||
|
assertThat(venues.size(), is(7)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @see DATAMONGO-360 |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void indexInfoIsCorrect() { |
||||||
|
|
||||||
|
IndexOperations operations = template.indexOps(Venue.class); |
||||||
|
List<IndexInfo> indexInfo = operations.getIndexInfo(); |
||||||
|
|
||||||
|
assertThat(indexInfo.size(), is(2)); |
||||||
|
|
||||||
|
List<IndexField> fields = indexInfo.get(0).getIndexFields(); |
||||||
|
assertThat(fields.size(), is(1)); |
||||||
|
assertThat(fields, hasItem(IndexField.create("_id", Direction.ASC))); |
||||||
|
|
||||||
|
fields = indexInfo.get(1).getIndexFields(); |
||||||
|
assertThat(fields.size(), is(1)); |
||||||
|
assertThat(fields, hasItem(IndexField.geo("location"))); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void createIndex() { |
||||||
|
template.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location").typed(GeoSpatialIndexType.GEO_2D)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void dropIndex() { |
||||||
|
template.indexOps(Venue.class).dropIndex("location_2d"); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue