Search in sources :

Example 6 with SearchHits

use of org.springframework.data.elasticsearch.core.SearchHits in project spring-data-elasticsearch by spring-projects.

the class ElasticsearchStringQuery method execute.

@Override
public Object execute(Object[] parameters) {
    Class<?> clazz = queryMethod.getResultProcessor().getReturnedType().getDomainType();
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
    StringQuery stringQuery = createQuery(accessor);
    Assert.notNull(stringQuery, "unsupported query");
    if (queryMethod.hasAnnotatedHighlight()) {
        stringQuery.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
    }
    IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
    Object result = null;
    if (isCountQuery()) {
        result = elasticsearchOperations.count(stringQuery, clazz, index);
    } else if (queryMethod.isPageQuery()) {
        stringQuery.setPageable(accessor.getPageable());
        SearchHits<?> searchHits = elasticsearchOperations.search(stringQuery, clazz, index);
        if (queryMethod.isSearchPageMethod()) {
            result = SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable());
        } else {
            result = SearchHitSupport.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, stringQuery.getPageable()));
        }
    } else if (queryMethod.isStreamQuery()) {
        stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
        result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(stringQuery, clazz, index));
    } else if (queryMethod.isCollectionQuery()) {
        stringQuery.setPageable(accessor.getPageable().isPaged() ? accessor.getPageable() : Pageable.unpaged());
        result = elasticsearchOperations.search(stringQuery, clazz, index);
    } else {
        result = elasticsearchOperations.searchOne(stringQuery, clazz, index);
    }
    return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod()) ? SearchHitSupport.unwrapSearchHits(result) : result;
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) StringQuery(org.springframework.data.elasticsearch.core.query.StringQuery) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Aggregations

SearchHits (org.springframework.data.elasticsearch.core.SearchHits)6 List (java.util.List)4 IndexCoordinates (org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)3 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Pageable (org.springframework.data.domain.Pageable)2 SearchHitsImpl (org.springframework.data.elasticsearch.core.SearchHitsImpl)2 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)2 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)2 Query (org.springframework.data.elasticsearch.core.query.Query)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 LocalDate (java.time.LocalDate)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Collectors.toList (java.util.stream.Collectors.toList)1 IntStream (java.util.stream.IntStream)1 Assertions (org.assertj.core.api.Assertions)1 Extension (org.eclipse.openvsx.entities.Extension)1 RepositoryService (org.eclipse.openvsx.repositories.RepositoryService)1