use of org.springframework.data.geo.GeoResult in project spring-data-mongodb by spring-projects.
the class MongoTemplate method geoNear.
public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String collectionName, Class<T> returnType) {
if (near == null) {
throw new InvalidDataAccessApiUsageException("NearQuery must not be null!");
}
if (domainType == null) {
throw new InvalidDataAccessApiUsageException("Entity class must not be null!");
}
Assert.notNull(collectionName, "CollectionName must not be null!");
Assert.notNull(returnType, "ReturnType must not be null!");
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(domainType);
Document nearDocument = near.toDocument();
Document command = new Document("geoNear", collection);
command.putAll(nearDocument);
if (nearDocument.containsKey("query")) {
Document query = (Document) nearDocument.get("query");
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(domainType)));
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}", serializeToJsonSafely(command), domainType, collectionName);
}
Document commandResult = executeCommand(command, this.readPreference);
List<Object> results = (List<Object>) commandResult.get("results");
results = results == null ? Collections.emptyList() : results;
DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<T>(new ProjectingReadCallback<>(mongoConverter, domainType, returnType, collectionName), near.getMetric());
List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());
int index = 0;
long elementsToSkip = near.getSkip() != null ? near.getSkip() : 0;
for (Object element : results) {
/*
* As MongoDB currently (2.4.4) doesn't support the skipping of elements in near queries
* we skip the elements ourselves to avoid at least the document 2 object mapping overhead.
*
* @see <a href="https://jira.mongodb.org/browse/SERVER-3925">MongoDB Jira: SERVER-3925</a>
*/
if (index >= elementsToSkip) {
result.add(callback.doWith((Document) element));
}
index++;
}
if (elementsToSkip > 0) {
// as we skipped some elements we have to calculate the averageDistance ourselves:
return new GeoResults<T>(result, near.getMetric());
}
GeoCommandStatistics stats = GeoCommandStatistics.from(commandResult);
return new GeoResults<T>(result, new Distance(stats.getAverageDistance(), near.getMetric()));
}
use of org.springframework.data.geo.GeoResult in project spring-data-mongodb by spring-projects.
the class MongoQueryExecutionUnitTests method pagingGeoExecutionShouldUseCountFromResultWithOffsetAndResultsWithinPageSize.
// DATAMONGO-1464
@Test
public void pagingGeoExecutionShouldUseCountFromResultWithOffsetAndResultsWithinPageSize() {
GeoResult<Person> result = new GeoResult<>(new Person(), DISTANCE);
when(findOperationMock.near(any(NearQuery.class))).thenReturn(terminatingGeoMock);
doReturn(new GeoResults<>(Arrays.asList(result, result, result, result))).when(terminatingGeoMock).all();
ConvertingParameterAccessor accessor = new ConvertingParameterAccessor(converter, new MongoParametersParameterAccessor(queryMethod, new Object[] { POINT, DISTANCE, PageRequest.of(0, 10) }));
PartTreeMongoQuery query = new PartTreeMongoQuery(queryMethod, mongoOperationsMock);
PagingGeoNearExecution execution = new PagingGeoNearExecution(findOperationMock, queryMethod, accessor, query);
execution.execute(new Query());
verify(terminatingGeoMock).all();
}
Aggregations