code: @SuppressWarnings("deprecation")
    private EntityCollectionResponse retrieveEntityCollectionInternal(
        String entityTypeId,
        EntityCollectionRequest request,
        Set<String> attributesSet,
        Map<String, Set<String>> attributeExpandsSet) {
      EntityType meta = dataService.getEntityType(entityTypeId);
      Repository<Entity> repository = dataService.getRepository(entityTypeId);
  
      // convert sort
      Sort sort;
      SortV1 sortV1 = request.getSort();
      if (sortV1 != null) {
        sort = new Sort();
        for (SortV1.OrderV1 orderV1 : sortV1) {
          sort.on(
              orderV1.getProperty(),
              orderV1.getDirection() == SortV1.DirectionV1.ASC
                  ? Sort.Direction.ASC
                  : Sort.Direction.DESC);
        }
      } else {
        sort = null;
      }
  
      List<QueryRule> queryRules = request.getQ() == null ? Collections.emptyList() : request.getQ();
      Query<Entity> q =
          new QueryImpl<>(queryRules)
              .pageSize(request.getNum())
              .offset(request.getStart())
              .sort(sort);
  
      Iterable<Entity> it = () -> dataService.findAll(entityTypeId, q).iterator();
      Long count = repository.count(new QueryImpl<>(q).setOffset(0).setPageSize(0));
      EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
  
      List<Map<String, Object>> entities = new ArrayList<>();
      for (Entity entity : it) {
        entities.add(getEntityAsMap(entity, meta, attributesSet, attributeExpandsSet));
      }
  
      return new EntityCollectionResponse(
          pager,
          entities,
          UriUtils.createEntityCollectionUriPath(entityTypeId),
          meta,
          permissionService,
          dataService);
    }
docs: Handles a Query