fix autocomplete search no full-text

This commit is contained in:
Yuri Kuznetsov
2022-05-09 16:03:54 +03:00
parent 7758e20224
commit ba3bbfc097
2 changed files with 34 additions and 14 deletions
@@ -29,14 +29,14 @@
namespace Espo\Core\Record;
use Espo\Core\{
Exceptions\Forbidden,
Exceptions\BadRequest,
Utils\Config,
Api\Request,
Select\SearchParams,
Utils\Json,
};
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Utils\Config;
use Espo\Core\Api\Request;
use Espo\Core\Select\SearchParams;
use Espo\Core\Select\Text\MetadataProvider as TextMetadataProvider;
use Espo\Core\Utils\Json;
use JsonException;
@@ -46,9 +46,12 @@ class SearchParamsFetcher
private Config $config;
public function __construct(Config $config)
private TextMetadataProvider $textMetadataProvider;
public function __construct(Config $config, TextMetadataProvider $textMetadataProvider)
{
$this->config = $config;
$this->textMetadataProvider = $textMetadataProvider;
}
public function fetch(Request $request): SearchParams
@@ -67,7 +70,7 @@ class SearchParamsFetcher
$this->fetchRawJsonSearchParams($request):
$this->fetchRawMultipleParams($request);
$this->handleRawParams($params);
$this->handleRawParams($params, $request);
return $params;
}
@@ -164,7 +167,7 @@ class SearchParamsFetcher
/**
* @param array<string,mixed> $params
*/
private function handleRawParams(array &$params): void
private function handleRawParams(array &$params, Request $request): void
{
if (isset($params['maxSize']) && !is_int($params['maxSize'])) {
throw new BadRequest('maxSize must be integer.');
@@ -178,7 +181,8 @@ class SearchParamsFetcher
strpos($q, '*') === false &&
strpos($q, '"') === false &&
strpos($q, '+') === false &&
strpos($q, '-') === false
strpos($q, '-') === false &&
$this->hasFullTextSearch($request)
) {
$params['q'] = $q . '*';
}
@@ -186,6 +190,21 @@ class SearchParamsFetcher
$this->handleMaxSize($params);
}
private function hasFullTextSearch(Request $request): bool
{
$scope = $request->getRouteParam('controller');
if (!$scope) {
return false;
}
if ($request->getRouteParam('action') !== 'index') {
return false;
}
return $this->textMetadataProvider->hasFullTextSearch($scope);
}
/**
* @param array<string,mixed> $params
*/
@@ -33,10 +33,10 @@ use Espo\Core\Record\SearchParamsFetcher;
use Espo\Core\Api\RequestWrapper;
use Espo\Core\Utils\Config;
use Espo\Core\Select\Text\MetadataProvider as TextMetadataProvider;
use Slim\Psr7\Factory\RequestFactory;
class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase
{
private $config;
@@ -44,6 +44,7 @@ class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase
protected function setUp(): void
{
$this->config = $this->createMocK(Config::class);
$this->textMetadataProvider = $this->createMocK(TextMetadataProvider::class);
$this->config
->method('get')
@@ -62,7 +63,7 @@ class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase
$request = (new RequestFactory)->createRequest('GET', 'http://localhost/?' . $q);
$fetcher = new SearchParamsFetcher($this->config);
$fetcher = new SearchParamsFetcher($this->config, $this->textMetadataProvider);
$params = $fetcher->fetch(new RequestWrapper($request));