Skip to content

Commit

Permalink
Merge pull request #6 from wkania/improve-search-and-tree-endpoints
Browse files Browse the repository at this point in the history
Search and tree endpoints refactor
  • Loading branch information
labudzinski authored Dec 12, 2023
2 parents 70b2bfa + dc1a3aa commit 219c7ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
28 changes: 6 additions & 22 deletions src/Controller/BaseEndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down
20 changes: 17 additions & 3 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.',
Expand Down Expand Up @@ -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();
Expand All @@ -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());
}
Expand All @@ -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()));
Expand Down
3 changes: 1 addition & 2 deletions src/Elasticsearch/Index/IndexQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use ONGR\ElasticsearchDSL\Search;
use Symfony\Component\HttpFoundation\Request;

final class IndexQueryService
{
public function __construct(private Client $client, private string $indexNamePrefix)
{
}

public function createSearch(Request $request): Search
public function createSearch(): Search
{
return new Search();
}
Expand Down

0 comments on commit 219c7ad

Please sign in to comment.