Search code examples
mongodbgeolocationgeospatialmorphianinjaframework

Ninjaframework, Morphia and GeoSpatial indexes


I am trying to store and query geographical coordinates in MongoDB. I am using Ninjaframework with the ninja-mongodb module, specifically using Morphia.

The entity with coordinates looks like this

@Entity
@Indexes({
    @Index(fields = @Field(value="coordinates", type = IndexType.GEO2DSPHERE))
})
public class Place {
    @Id
    public ObjectId id;
    public String google_places_id;
    public String name;
    public Point coordinates;
}

The code that creates the objects and queries them looks like this

try {
        List<models.lc.core.Place> lcPlaces = mongoDB.getDatastore().find(Place.class).field("coordinates").near(lat, lng).asList();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(   DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Places places = mapper.readValue(Http.Get(url), Places.class);
        for(models.google.Place place:places.results) {
            models.lc.core.Place p = new models.lc.core.Place();
            p.name = place.name;
            p.coordinates = GeoJson.point(place.geometry.location.lat, place.geometry.location.lng);
            p.google_places_id = place.place_id;
            mongoDB.save(p);
        }
        return new Result(HttpStatus.OK_200).json().render(places);
        //return new Result(HttpStatus.OK_200).text().render(Http.dumpInputStream(Http.Get(url)));
    }
    catch (Exception e){
        Places response = new Places();
        response.status = url;
        return new Result(HttpStatus.BAD_REQUEST_400).json().render(e);
    }

I get this error message

"errorMessage": "error processing query: ns=core-api.PlaceTree:   GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort:  {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
"errorCode": 2,
"message": "Query failed with error code 2 and error message 'error processing query: ns=core-api.PlaceTree: GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query' on server localhost:27017",
"localizedMessage": "Query failed with error code 2 and error message 'error processing query: ns=core-api.PlaceTree: GEONEAR  field=coordinates maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query' on server localhost:27017",
"suppressed": []

I have tried to call mongoDB.ensureIndexes(true); which does create the index on coordinates. But I still get the error I pasted above.

The internet provides no help, I hope someone out there can.

Many thanks in advance!


Solution

  • Can you file an issue with index definition created by morphia and the one you created by hand? If morphia is creating bad indexes, i'd like to get that fixed. Thanks.