/** * Creates {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} out of {@link GeoSpatialIndexed} for * {@link MongoPersistentProperty}. * * @param dotPath The properties {@literal "dot"} path representation from its document root. * @param collection * @param persistentProperty * @return */ @Nullable protected IndexDefinitionHolder createGeoSpatialIndexDefinition(String dotPath, String collection, MongoPersistentProperty persistentProperty) { GeoSpatialIndexed index = persistentProperty.findAnnotation(GeoSpatialIndexed.class); if (index == null) { return null; } GeospatialIndex indexDefinition = new GeospatialIndex(dotPath); indexDefinition.withBits(index.bits()); indexDefinition.withMin(index.min()).withMax(index.max()); if (!index.useGeneratedName()) { indexDefinition.named(pathAwareIndexName(index.name(), dotPath, persistentProperty)); } indexDefinition.typed(index.type()).withBucketSize(index.bucketSize()).withAdditionalField(index.additionalField()); return new IndexDefinitionHolder(dotPath, indexDefinition, collection); }
@Override public String toString() { return String.format("Geo index: %s - Options: %s", getIndexKeys(), getIndexOptions()); } }
mongoTemplate.indexOps(MyClass.class).ensureIndex(new GeospatialIndex("attribute.holding.coordinates")); NearQuery nearQuery = NearQuery.near(x, y); final GeoResults<MyClass> geoResults = mongoTemplate.geoNear(nearQuery, MyClass.class); return geoResults.getContent().stream().map(GeoResult::getContent).collect(Collectors.toList());
@Test public void serializePoinInCriteriaNearSphere() throws Exception { mongoTemplate.dropCollection(EventWithLocation.class); mongoTemplate.insert(new EventWithLocation(3, new Point(-73.99408, 40.75057), "42")); mongoTemplate.indexOps(EventWithLocation.class).ensureIndex(new GeospatialIndex("location")); Point p = new Point(-73, 40); NearQuery geoNear = NearQuery.near(p, Metrics.KILOMETERS).maxDistance(150.0); TypedAggregation<EventWithLocation> agg = newAggregation(EventWithLocation.class, // geoNear(geoNear, "distance") // , match(where("eventTime").gte(1)) // , sort(Direction.DESC, "eventTime") // , group("vanId").first(Aggregation.ROOT).as("first") // ); AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, DBObject.class); List<DBObject> list = results.getMappedResults(); DBObject firstResult = list.get(0); assertThat(firstResult.get("_id"), is(equalTo((Object)"42"))); }
/** * Creates {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} out of {@link GeoSpatialIndexed} for * {@link MongoPersistentProperty}. * * @param dotPath The properties {@literal "dot"} path representation from its document root. * @param collection * @param persistentProperty * @return */ @Nullable protected IndexDefinitionHolder createGeoSpatialIndexDefinition(String dotPath, String collection, MongoPersistentProperty persistentProperty) { GeoSpatialIndexed index = persistentProperty.findAnnotation(GeoSpatialIndexed.class); if (index == null) { return null; } GeospatialIndex indexDefinition = new GeospatialIndex(dotPath); indexDefinition.withBits(index.bits()); indexDefinition.withMin(index.min()).withMax(index.max()); if (!index.useGeneratedName()) { indexDefinition.named(pathAwareIndexName(index.name(), dotPath, persistentProperty)); } indexDefinition.typed(index.type()).withBucketSize(index.bucketSize()).withAdditionalField(index.additionalField()); return new IndexDefinitionHolder(dotPath, indexDefinition, collection); }
@Override public String toString() { return String.format("Geo index: %s - Options: %s", getIndexKeys(), getIndexOptions()); } }