Ninjaframework、Morphia 和 GeoSpatial 索引

Ninjaframework, Morphia and GeoSpatial indexes

我正在尝试在 MongoDB 中存储和查询地理坐标。我正在使用带有 ninja-mongodb 模块的 Ninjaframework,特别是使用 Morphia。

有坐标的实体是这样的

@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;
}

创建对象并查询它们的代码如下所示

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);
    }

我收到此错误消息

"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": []

我试过调用mongoDB.ensureIndexes(true);它确实在坐标上创建了索引。但是我仍然得到上面粘贴的错误。

互联网没有帮助,我希望有人可以。

非常感谢!

你能提交一份 issue 吗啡创建的索引定义和你手工创建的索引定义吗?如果吗啡正在创建错误的索引,我想修复它。谢谢