Skip to content

Commit

Permalink
Merge pull request #104 from ConductionNL/feature/DIMOC-124/search-page
Browse files Browse the repository at this point in the history
Search fixes
  • Loading branch information
bbrands02 authored Aug 6, 2024
2 parents c748415 + 9ee2eb9 commit 9e557e6
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 123 deletions.
39 changes: 9 additions & 30 deletions lib/Controller/CatalogiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OCA\OpenCatalogi\Db\CatalogMapper;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -35,46 +36,24 @@ public function page(?string $getParameter): TemplateResponse
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(ObjectService $objectService): JSONResponse
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->request->getParams();

$searchConditions = [];
$searchParams = [];
$fieldsToSearch = ['title', 'description', 'summary'];

foreach ($filters as $key => $value) {
if ($key === '_search') {
// MongoDB
$searchRegex = ['$regex' => $value, '$options' => 'i'];
$filters['$or'] = [];

// MySQL
$searchParams['search'] = '%' . strtolower($value) . '%';

foreach ($fieldsToSearch as $field) {
// MongoDB
$filters['$or'][] = [$field => $searchRegex];

// MySQL
$searchConditions[] = "LOWER($field) LIKE :search";
}
}

if (str_starts_with($key, '_')) {
unset($filters[$key]);
}
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
// Unset mongodb filter
unset($filters['$or']);
$searchParams = $searchService->createMySQLSearchParams($filters, $fieldsToSearch);
$searchConditions = $searchService->createMySQLSearchConditions($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters);

return new JSONResponse(['results' => $this->catalogMapper->findAll(filters: $filters, searchParams: $searchParams, searchConditions: $searchConditions)]);
}


$filters = $searchService->createMongoDBSearchFilter($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

try {
$dbConfig = [
'base_uri' => $this->config->getValueString($this->appName, 'mongodbLocation'),
Expand Down
39 changes: 9 additions & 30 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OCA\OpenCatalogi\Db\ListingMapper;
use OCA\OpenCatalogi\Service\DirectoryService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -82,46 +83,24 @@ public function add(?string $url, DirectoryService $directoryService): JSONRespo
* @PublicPage
* @NoCSRFRequired
*/
public function index(ObjectService $objectService): JSONResponse
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->request->getParams();

$searchConditions = [];
$searchParams = [];
$fieldsToSearch = ['summary'];

foreach ($filters as $key => $value) {
if ($key === '_search') {
// MongoDB
$searchRegex = ['$regex' => $value, '$options' => 'i'];
$filters['$or'] = [];

// MySQL
$searchParams['search'] = '%' . strtolower($value) . '%';

foreach ($fieldsToSearch as $field) {
// MongoDB
$filters['$or'][] = [$field => $searchRegex];

// MySQL
$searchConditions[] = "LOWER($field) LIKE :search";
}
}

if (str_starts_with($key, '_')) {
unset($filters[$key]);
}
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
// Unset mongodb filter
unset($filters['$or']);
$searchParams = $searchService->createMySQLSearchParams($filters, $fieldsToSearch);
$searchConditions = $searchService->createMySQLSearchConditions($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

return new JSONResponse(['results' => $this->listingMapper->findAll(filters: $filters, searchParams: $searchParams, searchConditions: $searchConditions)]);
}


$filters = $searchService->createMongoDBSearchFilter($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');
Expand Down
37 changes: 8 additions & 29 deletions lib/Controller/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OCA\OpenCatalogi\Db\MetaDataMapper;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -44,46 +45,24 @@ public function page(?string $getParameter)
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(ObjectService $objectService): JSONResponse
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->request->getParams();

$searchConditions = [];
$searchParams = [];
$fieldsToSearch = ['title', 'description'];

foreach ($filters as $key => $value) {
if ($key === '_search') {
// MongoDB
$searchRegex = ['$regex' => $value, '$options' => 'i'];
$filters['$or'] = [];

// MySQL
$searchParams['search'] = '%' . strtolower($value) . '%';

foreach ($fieldsToSearch as $field) {
// MongoDB
$filters['$or'][] = [$field => $searchRegex];

// MySQL
$searchConditions[] = "LOWER($field) LIKE :search";
}
}

if (str_starts_with($key, '_')) {
unset($filters[$key]);
}
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
// Unset mongodb filter
unset($filters['$or']);
$searchParams = $searchService->createMySQLSearchParams($filters, $fieldsToSearch);
$searchConditions = $searchService->createMySQLSearchConditions($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

return new JSONResponse(['results' =>$this->metaDataMapper->findAll(filters: $filters, searchParams: $searchParams, searchConditions: $searchConditions)]);
}

$filters = $searchService->createMongoDBSearchFilter($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');
Expand Down
39 changes: 9 additions & 30 deletions lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OCA\OpenCatalogi\Db\PublicationMapper;
use OCA\OpenCatalogi\Service\ElasticSearchService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -110,50 +111,28 @@ public function catalog(string|int $id): TemplateResponse
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(ObjectService $objectService): JSONResponse
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{
$filters = $this->request->getParams();

$searchConditions = [];
$searchParams = [];
$fieldsToSearch = ['title', 'description', 'summary'];

foreach ($filters as $key => $value) {
if ($key === '_search') {
// MongoDB
$searchRegex = ['$regex' => $value, '$options' => 'i'];
$filters['$or'] = [];

// MySQL
$searchParams['search'] = '%' . strtolower($value) . '%';

foreach ($fieldsToSearch as $field) {
// MongoDB
$filters['$or'][] = [$field => $searchRegex];

// MySQL
$searchConditions[] = "LOWER($field) LIKE :search";
}
}

if (str_starts_with($key, '_')) {
unset($filters[$key]);
}
}

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
// Unset mongodb filter
unset($filters['$or']);
$searchParams = $searchService->createMySQLSearchParams($filters, $fieldsToSearch);
$searchConditions = $searchService->createMySQLSearchConditions($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

return new JSONResponse(['results' => $this->publicationMapper->findAll(filters: $filters, searchParams: $searchParams, searchConditions: $searchConditions)]);
}

$filters = $searchService->createMongoDBSearchFilter($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters, $fieldsToSearch);

$dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation');
$dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey');
$dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster');

$filters['_schema'] = 'publication';

$result = $objectService->findObjects(filters: $filters, config: $dbConfig);
Expand Down
46 changes: 45 additions & 1 deletion lib/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\OpenCatalogi\Controller;

use OCA\OpenCatalogi\Service\ElasticSearchService;
use OCA\OpenCatalogi\Db\PublicationMapper;
use OCA\OpenCatalogi\Service\SearchService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
Expand All @@ -25,7 +26,11 @@ class SearchController extends Controller
]
];

public function __construct($appName, IRequest $request, private readonly IAppConfig $config)
public function __construct(
$appName,
IRequest $request,
private readonly PublicationMapper $publicationMapper,
private readonly IAppConfig $config)
{
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -64,6 +69,18 @@ public function index(SearchService $searchService): JSONResponse
$filters = $this->request->getParams();
unset($filters['_route']);

$fieldsToSearch = ['title', 'description', 'summary'];

if($this->config->hasKey($this->appName, 'mongoStorage') === false
|| $this->config->getValueString($this->appName, 'mongoStorage') !== '1'
) {
$searchParams = $searchService->createMySQLSearchParams($filters, $fieldsToSearch);
$searchConditions = $searchService->createMySQLSearchConditions($filters, $fieldsToSearch);
$filters = $searchService->unsetSpecialQueryParams($filters);

return new JSONResponse(['results' => $this->publicationMapper->findAll(filters: $filters, searchParams: $searchParams, searchConditions: $searchConditions)]);
}

//@TODO: find a better way to get query params. This fixes it for now.
$keys = array_keys(array: $filters);
$values = array_values(array: $filters);
Expand All @@ -72,6 +89,20 @@ public function index(SearchService $searchService): JSONResponse

$filters = array_combine(keys: $keys, values: $values);

$requiredElasticConfig = ['location', 'key', 'index'];
$missingFields = null;
foreach ($requiredElasticConfig as $key) {
if (isset($elasticConfig[$key]) === false || empty($elasticConfig[$key])) {
$missingFields .= "$key, ";
}
}

if ($missingFields !== null) {
$errorMessage = "Missing the following elastic configuration: {$missingFields}please update your elastic connection in application settings.";
$response = new JSONResponse(data: ['code' => 403, 'message' => $errorMessage], statusCode: 403);

return $response;
}

$data = $searchService->search(parameters: $filters, elasticConfig: $elasticConfig, dbConfig: $dbConfig);

Expand All @@ -94,6 +125,19 @@ public function show(string|int $id, SearchService $searchService): JSONResponse

$filters = ['_id' => (string) $id];

$requiredElasticConfig = ['location', 'key', 'index'];
$missingFields = null;
foreach ($requiredElasticConfig as $key) {
if (isset($elasticConfig[$key]) === false) {
$missingFields .= "$key ";
}
}

if ($missingFields !== null) {
$errorMessage = "Missing the following elastic configuration: {$missingFields}please update your elastic connection in application settings.";
return new JSONResponse(['message' => $errorMessage], 403);
}

$data = $searchService->search(parameters: $filters, elasticConfig: $elasticConfig, dbConfig: $dbConfig);

if(count($data['results']) > 0) {
Expand Down
Loading

0 comments on commit 9e557e6

Please sign in to comment.