DATAES-913 - Minor optimization on collection-returning derived queries.

Original PR: #509
This commit is contained in:
Peter-Josef Meisch 2020-08-23 15:28:50 +02:00 committed by GitHub
parent 368957f735
commit 4ef442966f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -48,7 +48,8 @@ public final class SearchHitSupport {
* @return a corresponding object where the SearchHits are replaced by their content if possible, otherwise the
* original object
*/
public static Object unwrapSearchHits(Object result) {
@Nullable
public static Object unwrapSearchHits(@Nullable Object result) {
if (result == null) {
return result;

View File

@ -15,10 +15,14 @@
*/
package org.springframework.data.elasticsearch.repository.query;
import java.util.Collections;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHitSupport;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchHitsImpl;
import org.springframework.data.elasticsearch.core.TotalHitsRelation;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
@ -28,6 +32,7 @@ import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.data.util.StreamUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@ -74,6 +79,7 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
Object result = null;
if (tree.isLimiting()) {
// noinspection ConstantConditions
query.setMaxResults(tree.getMaxResults());
}
@ -100,12 +106,19 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
if (accessor.getPageable().isUnpaged()) {
int itemCount = (int) elasticsearchOperations.count(query, clazz, index);
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
if (itemCount == 0) {
result = new SearchHitsImpl<>(0, TotalHitsRelation.EQUAL_TO, Float.NaN, null, Collections.emptyList(), null);
} else {
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
}
} else {
query.setPageable(accessor.getPageable());
}
result = elasticsearchOperations.search(query, clazz, index);
if (result == null) {
result = elasticsearchOperations.search(query, clazz, index);
}
} else if (tree.isCountProjection()) {
result = elasticsearchOperations.count(query, clazz, index);
@ -116,6 +129,7 @@ public class ElasticsearchPartQuery extends AbstractElasticsearchRepositoryQuery
return queryMethod.isNotSearchHitMethod() ? SearchHitSupport.unwrapSearchHits(result) : result;
}
@Nullable
private Object countOrGetDocumentsForDelete(CriteriaQuery query, ParametersParameterAccessor accessor) {
Object result = null;