diff --git a/src/Controller/BaseEndpointController.php b/src/Controller/BaseEndpointController.php index 4ebf477..a4b14f3 100644 --- a/src/Controller/BaseEndpointController.php +++ b/src/Controller/BaseEndpointController.php @@ -79,9 +79,9 @@ public function getDataObjectProvider(): DataObjectProvider public function applySearchSettings(Search $search): void { - $size = max((int) $this->request->get('size', 200), 1); - $pageCursor = max((int) $this->request->get('page_cursor', 0), 0); - $orderBy = $this->request->get('order_by'); + $size = max($this->request->query->getInt('size', 200), 1); + $pageCursor = max($this->request->query->getInt('page_cursor', 0), 0); + $orderBy = $this->request->query->getString('order_by', ''); $search->setSize($size); $search->setFrom($pageCursor); @@ -91,10 +91,9 @@ public function applySearchSettings(Search $search): void $this->nextPageCursor = $pageCursor + $size; } - private function applySort(Search $search, mixed $orderBy): void + private function applySort(Search $search, string $orderBy): void { - if (null !== $orderBy) { - $orderBy = (string)$orderBy; + if (!empty($orderBy)) { $items = json_decode($orderBy, true); if (is_array($items)) { foreach ($items as $field => $order) { @@ -121,9 +120,7 @@ private function applySort(Search $search, mixed $orderBy): void */ protected function applyQueriesAndAggregations(Search $search, ConfigReader $configReader): void { - $parentId = (int) $this->request->get('parentId', 1); - $type = $this->request->get('type', null); - $fulltext = $this->request->get('fulltext_search'); + $fulltext = $this->request->query->getString('fulltext_search'); /* * @TODO to remove on 2.2.x */ @@ -158,19 +155,6 @@ protected function applyQueriesAndAggregations(Search $search, ConfigReader $con $search->addAggregation(new TermsAggregation($field, $field)); } } - - $query['bool']['filter']['bool']['must'][] = [ - 'term' => [ - 'system.type' => $type, - ], - ]; - $query['bool']['filter']['bool']['must'][] = [ - 'term' => [ - 'system.parentId' => $parentId, - ], - ]; - - $body['query'] = $query; } /** diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 5566b57..cb86164 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -14,6 +14,7 @@ use CIHub\Bundle\SimpleRESTAdapterBundle\Elasticsearch\Index\IndexQueryService; use CIHub\Bundle\SimpleRESTAdapterBundle\Exception\InvalidParameterException; +use CIHub\Bundle\SimpleRESTAdapterBundle\Exception\NotFoundException; use CIHub\Bundle\SimpleRESTAdapterBundle\Manager\IndexManager; use CIHub\Bundle\SimpleRESTAdapterBundle\Reader\ConfigReader; use CIHub\Bundle\SimpleRESTAdapterBundle\Traits\ListingFilterTrait; @@ -289,7 +290,7 @@ public function searchAction(Request $request, IndexManager $indexManager, Index ); } - $search = $indexService->createSearch($request); + $search = $indexService->createSearch(); $this->applySearchSettings($search); $this->applyQueriesAndAggregations($search, $configReader); @@ -384,6 +385,16 @@ enum: ['asset', 'object'] type: 'string' ) ), + new OA\Parameter( + name: 'include_folders', + description: 'Set to true to include folders, default false.', + in: 'query', + required: false, + schema: new OA\Schema( + type: 'boolean', + default: false + ) + ), new OA\Parameter( name: 'include_aggs', description: 'Set to true to include aggregation information, default false.', @@ -498,7 +509,7 @@ enum: ['asset', 'object'] ), ], )] - public function treeItemsAction(IndexManager $indexManager, IndexQueryService $indexService, Request $request): JsonResponse + public function treeItemsAction(IndexManager $indexManager, IndexQueryService $indexService): JsonResponse { $this->authManager->checkAuthentication(); $configuration = $this->getDataHubConfiguration(); @@ -517,6 +528,9 @@ public function treeItemsAction(IndexManager $indexManager, IndexQueryService $i $this->checkRequiredParameters(['type' => $type]); $root = Service::getElementById($type, $id); + if (!$root) { + throw new NotFoundException(sprintf("Parent with id [%s] doesn't exist for the type [%s]", $id, $type)); + } if (!$root->isAllowed('list', $this->user)) { throw new AccessDeniedHttpException('Missing the permission to list in the folder: '.$root->getRealFullPath()); } @@ -538,7 +552,7 @@ public function treeItemsAction(IndexManager $indexManager, IndexQueryService $i $indices[] = $indexManager->getIndexName(IndexManager::INDEX_OBJECT_FOLDER, $this->config); } } - $search = $indexService->createSearch($request); + $search = $indexService->createSearch(); $this->applySearchSettings($search); $this->applyQueriesAndAggregations($search, $configReader); $search->addQuery(new MatchQuery('system.parentId', $root->getId())); diff --git a/src/Elasticsearch/Index/IndexQueryService.php b/src/Elasticsearch/Index/IndexQueryService.php index 1c264a7..7f485d8 100644 --- a/src/Elasticsearch/Index/IndexQueryService.php +++ b/src/Elasticsearch/Index/IndexQueryService.php @@ -17,7 +17,6 @@ use Elastic\Elasticsearch\Exception\MissingParameterException; use Elastic\Elasticsearch\Exception\ServerResponseException; use ONGR\ElasticsearchDSL\Search; -use Symfony\Component\HttpFoundation\Request; final class IndexQueryService { @@ -25,7 +24,7 @@ public function __construct(private Client $client, private string $indexNamePre { } - public function createSearch(Request $request): Search + public function createSearch(): Search { return new Search(); }