Search in sources :

Example 11 with Pageable

use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.

the class DefaultFindSliceInterceptor method intercept.

@SuppressWarnings("unchecked")
@Override
public R intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, R> context) {
    if (context.hasAnnotation(Query.class)) {
        PreparedQuery<?, ?> preparedQuery = prepareQuery(methodKey, context);
        Pageable pageable = preparedQuery.getPageable();
        Iterable<R> iterable = (Iterable<R>) operations.findAll(preparedQuery);
        Slice<R> slice = Slice.of(CollectionUtils.iterableToList(iterable), pageable);
        return convertOrFail(context, slice);
    } else {
        PagedQuery<Object> pagedQuery = getPagedQuery(context);
        Iterable iterable = operations.findAll(pagedQuery);
        Slice<R> slice = Slice.of(CollectionUtils.iterableToList(iterable), pagedQuery.getPageable());
        return convertOrFail(context, slice);
    }
}
Also used : Pageable(io.micronaut.data.model.Pageable)

Example 12 with Pageable

use of io.micronaut.data.model.Pageable in project kestra by kestra-io.

the class ElasticSearchExecutionRepository method findTaskRun.

@Override
public ArrayListTotal<TaskRun> findTaskRun(String query, Pageable pageable, @Nullable List<State.Type> state) {
    BoolQueryBuilder filterAggQuery = QueryBuilders.boolQuery().filter(QueryBuilders.queryStringQuery(query));
    if (state != null) {
        filterAggQuery = filterAggQuery.must(QueryBuilders.termsQuery("taskRunList.state.current", stateConvert(state)));
    }
    NestedAggregationBuilder nestedAgg = AggregationBuilders.nested("NESTED", "taskRunList").subAggregation(AggregationBuilders.filter("FILTER", filterAggQuery).subAggregation(AggregationBuilders.topHits("TOPHITS").size(pageable.getSize()).sorts(defaultSorts(pageable, true)).from(Math.toIntExact(pageable.getOffset() - pageable.getSize()))));
    BoolQueryBuilder mainQuery = this.defaultFilter().filter(QueryBuilders.nestedQuery("taskRunList", QueryBuilders.queryStringQuery(query).field("*.fulltext"), ScoreMode.Total));
    SearchSourceBuilder sourceBuilder = this.searchSource(mainQuery, Optional.of(List.of(nestedAgg)), null).fetchSource(false);
    SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        ParsedNested pn = searchResponse.getAggregations().get("NESTED");
        Filter fa = pn.getAggregations().get("FILTER");
        long docCount = fa.getDocCount();
        TopHits th = fa.getAggregations().get("TOPHITS");
        List<TaskRun> collect = Arrays.stream(th.getHits().getHits()).map(documentFields -> {
            try {
                return MAPPER.readValue(documentFields.getSourceAsString(), TaskRun.class);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
        return new ArrayListTotal<>(collect, docCount);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TaskRun(io.kestra.core.models.executions.TaskRun) RequestOptions(org.opensearch.client.RequestOptions) java.util(java.util) ExecutionRepositoryInterface(io.kestra.core.repositories.ExecutionRepositoryInterface) Flow(io.kestra.core.models.executions.statistics.Flow) ParsedStringTerms(org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms) ZonedDateTime(java.time.ZonedDateTime) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) DateHistogramInterval(org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval) ExecutionCount(io.kestra.core.models.executions.statistics.ExecutionCount) ParsedDateHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram) State(io.kestra.core.models.flows.State) LongBounds(org.opensearch.search.aggregations.bucket.histogram.LongBounds) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) FiltersAggregator(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator) Nullable(io.micronaut.core.annotation.Nullable) Duration(java.time.Duration) Pageable(io.micronaut.data.model.Pageable) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) ParsedFilters(org.opensearch.search.aggregations.bucket.filter.ParsedFilters) QueryBuilders(org.opensearch.index.query.QueryBuilders) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) TopHits(org.opensearch.search.aggregations.metrics.TopHits) Singleton(jakarta.inject.Singleton) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) ScoreMode(org.apache.lucene.search.join.ScoreMode) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) DailyExecutionStatistics(io.kestra.core.models.executions.statistics.DailyExecutionStatistics) ZoneId(java.time.ZoneId) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) ParsedStats(org.opensearch.search.aggregations.metrics.ParsedStats) ArrayListTotal(io.kestra.core.repositories.ArrayListTotal) ExecutorsUtils(io.kestra.core.utils.ExecutorsUtils) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) IndexSettings(org.opensearch.index.IndexSettings) ModelValidator(io.kestra.core.models.validations.ModelValidator) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) Inject(jakarta.inject.Inject) ParsedNested(org.opensearch.search.aggregations.bucket.nested.ParsedNested) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchRequest(org.opensearch.action.search.SearchRequest) ParsedNested(org.opensearch.search.aggregations.bucket.nested.ParsedNested) IOException(java.io.IOException) ArrayListTotal(io.kestra.core.repositories.ArrayListTotal) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) TaskRun(io.kestra.core.models.executions.TaskRun) TopHits(org.opensearch.search.aggregations.metrics.TopHits) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder)

Example 13 with Pageable

use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.

the class HibernateJpaOperations method findAll.

@NonNull
@Override
public <T, R> Iterable<R> findAll(@NonNull PreparedQuery<T, R> preparedQuery) {
    return transactionOperations.executeRead(status -> {
        Session entityManager = sessionFactory.getCurrentSession();
        String queryStr = preparedQuery.getQuery();
        Pageable pageable = preparedQuery.getPageable();
        if (pageable != Pageable.UNPAGED) {
            Sort sort = pageable.getSort();
            if (sort.isSorted()) {
                queryStr += QUERY_BUILDER.buildOrderBy(queryStr, getEntity(preparedQuery.getRootEntity()), sort).getQuery();
            }
        }
        if (preparedQuery.isDtoProjection()) {
            Query<Tuple> q;
            if (preparedQuery.isNative()) {
                q = entityManager.createNativeQuery(queryStr, Tuple.class);
            } else if (queryStr.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
                Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
                Query<R> query = entityManager.createQuery(queryStr, wrapperType);
                bindPreparedQuery(query, preparedQuery, entityManager, queryStr);
                return query.list();
            } else {
                q = entityManager.createQuery(queryStr, Tuple.class);
            }
            bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
            return q.stream().map(tuple -> {
                Set<String> properties = tuple.getElements().stream().map(TupleElement::getAlias).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
                return (new BeanIntrospectionMapper<Tuple, R>() {

                    @Override
                    public Object read(Tuple tuple1, String alias) {
                        if (!properties.contains(alias)) {
                            return null;
                        }
                        return tuple1.get(alias);
                    }

                    @Override
                    public ConversionService<?> getConversionService() {
                        return dataConversionService;
                    }
                }).map(tuple, preparedQuery.getResultType());
            }).collect(Collectors.toList());
        } else {
            Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
            Query<R> q;
            if (preparedQuery.isNative()) {
                if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
                    q = entityManager.createNativeQuery(queryStr, wrapperType);
                } else {
                    q = entityManager.createNativeQuery(queryStr);
                }
            } else {
                q = entityManager.createQuery(queryStr, wrapperType);
            }
            bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
            return q.list();
        }
    });
}
Also used : DataType(io.micronaut.data.model.DataType) Connection(java.sql.Connection) ArrayUtils(io.micronaut.core.util.ArrayUtils) ExecutorReactiveOperations(io.micronaut.data.runtime.operations.ExecutorReactiveOperations) SubGraph(org.hibernate.graph.SubGraph) EachBean(io.micronaut.context.annotation.EachBean) StoredQuery(io.micronaut.data.model.runtime.StoredQuery) Locale(java.util.Locale) Map(java.util.Map) Query(org.hibernate.query.Query) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) DeleteOperation(io.micronaut.data.model.runtime.DeleteOperation) TypeHint(io.micronaut.core.annotation.TypeHint) AsyncCapableRepository(io.micronaut.data.operations.async.AsyncCapableRepository) Path(javax.persistence.criteria.Path) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) JpaRepositoryOperations(io.micronaut.data.jpa.operations.JpaRepositoryOperations) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) Graph(org.hibernate.graph.Graph) Sort(io.micronaut.data.model.Sort) RuntimePersistentProperty(io.micronaut.data.model.runtime.RuntimePersistentProperty) Collection(java.util.Collection) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) QueryHint(io.micronaut.data.annotation.QueryHint) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) Serializable(java.io.Serializable) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Order(javax.persistence.criteria.Order) Optional(java.util.Optional) TupleElement(javax.persistence.TupleElement) Named(jakarta.inject.Named) JpaQueryBuilder(io.micronaut.data.model.query.builder.jpa.JpaQueryBuilder) QueryParameterBinding(io.micronaut.data.model.runtime.QueryParameterBinding) AttributeNode(org.hibernate.graph.AttributeNode) RootGraph(org.hibernate.graph.RootGraph) Parameter(io.micronaut.context.annotation.Parameter) TransactionOperations(io.micronaut.transaction.TransactionOperations) Creator(io.micronaut.core.annotation.Creator) BeanProperty(io.micronaut.core.beans.BeanProperty) Session(org.hibernate.Session) FlushModeType(javax.persistence.FlushModeType) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) HibernatePresenceCondition(io.micronaut.jdbc.spring.HibernatePresenceCondition) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ApplicationContext(io.micronaut.context.ApplicationContext) Nullable(io.micronaut.core.annotation.Nullable) NativeQuery(org.hibernate.query.NativeQuery) Page(io.micronaut.data.model.Page) Pageable(io.micronaut.data.model.Pageable) ReactiveCapableRepository(io.micronaut.data.operations.reactive.ReactiveCapableRepository) ExecutorAsyncOperations(io.micronaut.data.runtime.operations.ExecutorAsyncOperations) Argument(io.micronaut.core.type.Argument) Expression(javax.persistence.criteria.Expression) ConversionService(io.micronaut.core.convert.ConversionService) BeanWrapper(io.micronaut.core.beans.BeanWrapper) ReactiveRepositoryOperations(io.micronaut.data.operations.reactive.ReactiveRepositoryOperations) Root(javax.persistence.criteria.Root) ExecutorService(java.util.concurrent.ExecutorService) Tuple(javax.persistence.Tuple) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) Iterator(java.util.Iterator) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) EntityManager(javax.persistence.EntityManager) InsertBatchOperation(io.micronaut.data.model.runtime.InsertBatchOperation) ReflectionUtils(io.micronaut.core.reflect.ReflectionUtils) NonNull(io.micronaut.core.annotation.NonNull) CollectionUtils(io.micronaut.core.util.CollectionUtils) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) DeleteBatchOperation(io.micronaut.data.model.runtime.DeleteBatchOperation) InsertOperation(io.micronaut.data.model.runtime.InsertOperation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) EntityGraph(io.micronaut.data.jpa.annotation.EntityGraph) BeanIntrospectionMapper(io.micronaut.data.runtime.mapper.BeanIntrospectionMapper) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) UpdateBatchOperation(io.micronaut.data.model.runtime.UpdateBatchOperation) UpdateOperation(io.micronaut.data.model.runtime.UpdateOperation) Collections(java.util.Collections) Type(org.hibernate.type.Type) Set(java.util.Set) TreeSet(java.util.TreeSet) StoredQuery(io.micronaut.data.model.runtime.StoredQuery) Query(org.hibernate.query.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) PreparedQuery(io.micronaut.data.model.runtime.PreparedQuery) NativeQuery(org.hibernate.query.NativeQuery) PagedQuery(io.micronaut.data.model.runtime.PagedQuery) Pageable(io.micronaut.data.model.Pageable) TupleElement(javax.persistence.TupleElement) ConversionService(io.micronaut.core.convert.ConversionService) DataConversionService(io.micronaut.data.runtime.convert.DataConversionService) Sort(io.micronaut.data.model.Sort) Tuple(javax.persistence.Tuple) Session(org.hibernate.Session) NonNull(io.micronaut.core.annotation.NonNull)

Example 14 with Pageable

use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.

the class FindPageSpecificationInterceptor method intercept.

@Override
public Page intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
    if (context.getParameterValues().length != 2) {
        throw new IllegalStateException("Expected exactly 2 arguments to method");
    }
    Specification specification = getSpecification(context);
    final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
    final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    Class<Object> rootEntity = getRequiredRootEntity(context);
    final CriteriaQuery<Object> query = criteriaBuilder.createQuery(rootEntity);
    final Root<Object> root = query.from(rootEntity);
    final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
    if (predicate != null) {
        query.where(predicate);
    }
    query.select(root);
    Pageable pageable = getPageable(context);
    final Sort sort = pageable.getSort();
    if (sort.isSorted()) {
        query.orderBy(getOrders(sort, root, criteriaBuilder));
    }
    final TypedQuery<Object> typedQuery = entityManager.createQuery(query);
    if (pageable.isUnpaged()) {
        List<Object> resultList = typedQuery.getResultList();
        return Page.of(resultList, pageable, resultList.size());
    } else {
        typedQuery.setFirstResult((int) pageable.getOffset());
        typedQuery.setMaxResults(pageable.getSize());
        final List<Object> results = typedQuery.getResultList();
        final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
        final Root<?> countRoot = countQuery.from(rootEntity);
        final Predicate countPredicate = specification.toPredicate(countRoot, countQuery, criteriaBuilder);
        if (countPredicate != null) {
            countQuery.where(countPredicate);
        }
        countQuery.select(criteriaBuilder.count(countRoot));
        Long singleResult = entityManager.createQuery(countQuery).getSingleResult();
        return Page.of(results, pageable, singleResult);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Specification(io.micronaut.data.jpa.repository.criteria.Specification) Predicate(javax.persistence.criteria.Predicate) EntityManager(javax.persistence.EntityManager) Pageable(io.micronaut.data.model.Pageable) Sort(io.micronaut.data.model.Sort)

Example 15 with Pageable

use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.

the class PageableRequestArgumentBinder method bind.

@Override
public BindingResult<Pageable> bind(ArgumentConversionContext<Pageable> context, HttpRequest<?> source) {
    HttpParameters parameters = source.getParameters();
    int page = Math.max(parameters.getFirst(configuration.getPageParameterName(), Integer.class).orElse(0), 0);
    final int configuredMaxSize = configuration.getMaxPageSize();
    final int defaultSize = configuration.getDefaultPageSize();
    int size = Math.min(parameters.getFirst(configuration.getSizeParameterName(), Integer.class).orElse(defaultSize), configuredMaxSize);
    String sortParameterName = configuration.getSortParameterName();
    boolean hasSort = parameters.contains(sortParameterName);
    Pageable pageable;
    Sort sort = null;
    if (hasSort) {
        List<String> sortParams = parameters.getAll(sortParameterName);
        List<Sort.Order> orders = sortParams.stream().map(sortMapper).collect(Collectors.toList());
        sort = Sort.of(orders);
    }
    if (size < 1) {
        if (page == 0 && configuredMaxSize < 1 && sort == null) {
            pageable = Pageable.UNPAGED;
        } else {
            pageable = Pageable.from(page, defaultSize, sort);
        }
    } else {
        pageable = Pageable.from(page, size, sort);
    }
    return () -> Optional.of(pageable);
}
Also used : Pageable(io.micronaut.data.model.Pageable) HttpParameters(io.micronaut.http.HttpParameters) Sort(io.micronaut.data.model.Sort)

Aggregations

Pageable (io.micronaut.data.model.Pageable)20 Sort (io.micronaut.data.model.Sort)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 PreparedQuery (io.micronaut.data.model.runtime.PreparedQuery)6 Collectors (java.util.stream.Collectors)6 NonNull (io.micronaut.core.annotation.NonNull)5 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)5 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 Internal (io.micronaut.core.annotation.Internal)4 StoredQuery (io.micronaut.data.model.runtime.StoredQuery)4 MongoFindOptions (io.micronaut.data.mongodb.operations.options.MongoFindOptions)4 DataConversionService (io.micronaut.data.runtime.convert.DataConversionService)4 EachBean (io.micronaut.context.annotation.EachBean)3 Parameter (io.micronaut.context.annotation.Parameter)3 Nullable (io.micronaut.core.annotation.Nullable)3 Page (io.micronaut.data.model.Page)3 Bson (org.bson.conversions.Bson)3 Session (org.hibernate.Session)3 BeanProperty (io.micronaut.core.beans.BeanProperty)2