Search in sources :

Example 1 with GeoResult

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()));
}
Also used : GeoResults(org.springframework.data.geo.GeoResults) Document(org.bson.Document) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) GeoResult(org.springframework.data.geo.GeoResult) Distance(org.springframework.data.geo.Distance)

Example 2 with GeoResult

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();
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) PagingGeoNearExecution(org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagingGeoNearExecution) FindWithQuery(org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) GeoResult(org.springframework.data.geo.GeoResult) Person(org.springframework.data.mongodb.repository.Person) Test(org.junit.Test)

Aggregations

GeoResult (org.springframework.data.geo.GeoResult)2 Document (org.bson.Document)1 Test (org.junit.Test)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 Distance (org.springframework.data.geo.Distance)1 GeoResults (org.springframework.data.geo.GeoResults)1 FindWithQuery (org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery)1 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)1 Query (org.springframework.data.mongodb.core.query.Query)1 Person (org.springframework.data.mongodb.repository.Person)1 PagingGeoNearExecution (org.springframework.data.mongodb.repository.query.MongoQueryExecution.PagingGeoNearExecution)1