I am using Spring Data Mongodb and trying to do some geo queries.
My code on the Service:
public void addLocation(UserLocation userLocation) {
if (!mongoTemplate.collectionExists(UserLocation.class)) {
mongoTemplate.createCollection(UserLocation.class);
}
mongoTemplate.indexOps(UserLocation.class).ensureIndex( new GeospatialIndex("location") );
userLocation.setId(UUID.randomUUID().toString());
mongoTemplate.insert(userLocation, COLLECTION_LOCATION);
}
where location field is a double [2] array.
When I try to make the query:
public List<UserLocation> getUserNearLocation(Point p, double min, double max){
NearQuery query = NearQuery.near(p).minDistance(new Distance(min, Metrics.KILOMETERS)).maxDistance(new Distance(max, Metrics.KILOMETERS));
// NearQuery query = NearQuery.near(p).maxDistance(new Distance(max, Metrics.KILOMETERS));
GeoResults<UserLocation> results = mongoTemplate.geoNear(query, UserLocation.class);
List<UserLocation> all = new ArrayList<UserLocation>();
Iterator<GeoResult<UserLocation>> iter = results.iterator();
while (iter.hasNext()){
GeoResult<UserLocation> temp = iter.next();
all.add(temp.getContent());
}
return all;
}
I get:
WARN : org.springframework.data.mongodb.core.MongoTemplate - Command execution of { "geoNear" : "userLocation" , "maxDistance" : 3.135711885774796E-4 , "minDistance" : 0.0 , "distanceMultiplier" : 6378.137 , "near" : [ 40.348553 , 18.179866] , "spherical" : true} failed: exception: minDistance doesn't work on 2d index
I am using Spring Data Mongo 1.7.0 RELEASE, because I saw this issue :http://ift.tt/1PnirYY but it is resolved in release 1.7.0
Aucun commentaire:
Enregistrer un commentaire