From ba02164d0b676e377a58b4e825894020de9ebd48 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 13 Apr 2022 20:00:15 +0300 Subject: [PATCH] ft changes --- .../Select/Applier/Appliers/TextFilter.php | 41 ++++--------------- .../Core/Select/Text/FullTextSearch/Mode.php | 37 +++++++++++++++++ .../Core/Select/Text/FullTextSearchData.php | 24 ++++++++++- .../Text/FullTextSearchDataComposer.php | 18 ++++++-- .../Select/Text/FullTextSearchDataTest.php | 7 ++-- 5 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 application/Espo/Core/Select/Text/FullTextSearch/Mode.php diff --git a/application/Espo/Core/Select/Applier/Appliers/TextFilter.php b/application/Espo/Core/Select/Applier/Appliers/TextFilter.php index 737b975ce5..367c6a4e5e 100644 --- a/application/Espo/Core/Select/Applier/Appliers/TextFilter.php +++ b/application/Espo/Core/Select/Applier/Appliers/TextFilter.php @@ -107,28 +107,13 @@ class TextFilter $filter = str_replace('*', '%', $filter); } - $filterForFullTextSearch = str_replace('%', '*', $filterOriginal); - - $skipFullTextSearch = false; - - if (!$forceFullTextSearch) { - if (mb_strpos($filterForFullTextSearch, '*') === 0) { - $skipFullTextSearch = true; - } - else if (mb_strpos($filterForFullTextSearch, ' *') !== false) { - $skipFullTextSearch = true; - } - } - - if ($params->noFullTextSearch()) { - $skipFullTextSearch = true; - } - $fullTextSearchData = null; - if (!$skipFullTextSearch) { + if (!$params->noFullTextSearch()) { $fullTextSearchIsAuxiliary = !$preferFullTextSearch; + $filterForFullTextSearch = str_replace('%', '*', $filterOriginal); + $fullTextSearchData = $this->getFullTextSearchData( $filterForFullTextSearch, $fullTextSearchIsAuxiliary @@ -141,21 +126,13 @@ class TextFilter $fullTextSearchFieldList = $fullTextSearchData ? $fullTextSearchData->getFieldList() : []; - $fieldList = array_filter( - $this->metadataProvider->getTextFilterAttributeList($this->entityType) ?? ['name'], - function ($field) use ($fullTextSearchFieldList, $forceFullTextSearch) { - if ($forceFullTextSearch) { - return false; + $fieldList = $forceFullTextSearch ? [] : + array_filter( + $this->metadataProvider->getTextFilterAttributeList($this->entityType) ?? ['name'], + function ($field) use ($fullTextSearchFieldList) { + return !in_array($field, $fullTextSearchFieldList); } - - // @todo Check this logic. - if (in_array($field, $fullTextSearchFieldList)) { - return false; - } - - return true; - } - ); + ); $filterData = FilterData::create($filter, $fieldList) ->withSkipWildcards($skipWildcards) diff --git a/application/Espo/Core/Select/Text/FullTextSearch/Mode.php b/application/Espo/Core/Select/Text/FullTextSearch/Mode.php new file mode 100644 index 0000000000..917c429fe1 --- /dev/null +++ b/application/Espo/Core/Select/Text/FullTextSearch/Mode.php @@ -0,0 +1,37 @@ +expression = $expression; $this->fieldList = $fieldList; $this->columnList = $columnList; + $this->mode = $mode; + + if (!in_array($mode, [Mode::NATURAL_LANGUAGE, Mode::BOOLEAN])) { + throw new InvalidArgumentException("Bad mode."); + } } public function getExpression(): Expression @@ -77,4 +91,12 @@ class FullTextSearchData { return $this->columnList; } + + /** + * @return Mode::* + */ + public function getMode(): string + { + return $this->mode; + } } diff --git a/application/Espo/Core/Select/Text/FullTextSearchDataComposer.php b/application/Espo/Core/Select/Text/FullTextSearchDataComposer.php index 01c06df8d1..2cc85fbb1f 100644 --- a/application/Espo/Core/Select/Text/FullTextSearchDataComposer.php +++ b/application/Espo/Core/Select/Text/FullTextSearchDataComposer.php @@ -30,6 +30,7 @@ namespace Espo\Core\Select\Text; use Espo\Core\Utils\Config; +use Espo\Core\Select\Text\FullTextSearch\Mode; use Espo\ORM\Query\Part\Expression\Util as ExpressionUtil; use Espo\ORM\Query\Part\Expression; @@ -42,6 +43,14 @@ class FullTextSearchDataComposer private MetadataProvider $metadataProvider; + /** + * @var array + */ + private array $functionMap = [ + Mode::BOOLEAN => 'MATCH_BOOLEAN', + Mode::NATURAL_LANGUAGE => 'MATCH_NATURAL_LANGUAGE', + ]; + public function __construct( string $entityType, Config $config, @@ -99,7 +108,7 @@ class FullTextSearchDataComposer $preparedFilter = $this->prepareFilter($filter, $params); - $function = 'MATCH_BOOLEAN'; + $mode = Mode::BOOLEAN; if ( $params->isAuxiliaryUse() && mb_strpos($preparedFilter, '*') === false @@ -109,7 +118,7 @@ class FullTextSearchDataComposer mb_strpos($preparedFilter, '-') === false && mb_strpos($preparedFilter, '*') === false ) { - $function = 'MATCH_NATURAL_LANGUAGE'; + $mode = Mode::NATURAL_LANGUAGE; } $argumentList = array_merge( @@ -122,12 +131,15 @@ class FullTextSearchDataComposer [$preparedFilter] ); + $function = $this->functionMap[$mode]; + $expression = ExpressionUtil::composeFunction($function, ...$argumentList); return new FullTextSearchData( $expression, $fieldList, - $columnList + $columnList, + $mode ); } diff --git a/tests/unit/Espo/Core/Select/Text/FullTextSearchDataTest.php b/tests/unit/Espo/Core/Select/Text/FullTextSearchDataTest.php index e57f2190fc..a3b7b5a042 100644 --- a/tests/unit/Espo/Core/Select/Text/FullTextSearchDataTest.php +++ b/tests/unit/Espo/Core/Select/Text/FullTextSearchDataTest.php @@ -30,17 +30,18 @@ namespace tests\unit\Espo\Core\Select\Text; use Espo\ORM\Query\Part\Expression as Expr; - use Espo\Core\Select\Text\FullTextSearchData; +use Espo\Core\Select\Text\FullTextSearch\Mode; class FullTextSearchDataTest extends \PHPUnit\Framework\TestCase { - public function testGet1() + public function testGet1(): void { - $item = new FullTextSearchData(Expr::create('TEST:()'), ['test'], ['column']); + $item = new FullTextSearchData(Expr::create('TEST:()'), ['test'], ['column'], Mode::BOOLEAN); $this->assertEquals('TEST:()', $item->getExpression()->getValue()); $this->assertEquals(['test'], $item->getFieldList()); $this->assertEquals(['column'], $item->getColumnList()); + $this->assertEquals(Mode::BOOLEAN, $item->getMode()); } }