ft changes

This commit is contained in:
Yuri Kuznetsov
2022-04-13 20:00:15 +03:00
parent 0cdbe89f5c
commit ba02164d0b
5 changed files with 88 additions and 39 deletions
@@ -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)
@@ -0,0 +1,37 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Select\Text\FullTextSearch;
class Mode
{
public const NATURAL_LANGUAGE = 'NATURAL_LANGUAGE';
public const BOOLEAN = 'BOOLEAN';
}
@@ -31,6 +31,9 @@ namespace Espo\Core\Select\Text;
use Espo\ORM\Query\Part\Expression;
use Espo\Core\Select\Text\FullTextSearch\Mode;
use InvalidArgumentException;
class FullTextSearchData
{
@@ -46,15 +49,26 @@ class FullTextSearchData
*/
private array $columnList;
/**
* @var Mode::* $mode
*/
private string $mode;
/**
* @param string[] $fieldList
* @param string[] $columnList
* @param Mode::* $mode
*/
public function __construct(Expression $expression, array $fieldList, array $columnList)
public function __construct(Expression $expression, array $fieldList, array $columnList, string $mode)
{
$this->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;
}
}
@@ -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<Mode::*,string>
*/
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
);
}
@@ -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());
}
}