-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
2,403 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/module-elasticsuite-core/Test/Unit/Search/Request/SortOrder/ScriptTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Request; | ||
|
||
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Script as ScriptSortOrder; | ||
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface; | ||
|
||
/** | ||
* Script sort order unit testing. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
*/ | ||
class ScriptTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Test basic script order. | ||
* | ||
* @return void | ||
*/ | ||
public function testBasicScriptSortOrder() | ||
{ | ||
$scriptType = 'number'; | ||
$scriptLang = 'painless'; | ||
$scriptSource = "doc['sortField'].value * params.factor"; | ||
|
||
$scriptSorOrder = new ScriptSortOrder($scriptType, $scriptLang, $scriptSource); | ||
|
||
// Default values. | ||
$this->assertNull($scriptSorOrder->getName()); | ||
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField()); | ||
$this->assertEquals(SortOrderInterface::SORT_ASC, $scriptSorOrder->getDirection()); | ||
$this->assertEquals(ScriptSortOrder::TYPE_SCRIPT, $scriptSorOrder->getType()); | ||
$this->assertEquals(SortOrderInterface::MISSING_LAST, $scriptSorOrder->getMissing()); | ||
|
||
$this->assertEquals( | ||
[ | ||
'lang' => $scriptLang, | ||
'source' => $scriptSource, | ||
'params' => [], | ||
], | ||
$scriptSorOrder->getScript(), | ||
); | ||
$this->assertEquals($scriptType, $scriptSorOrder->getScriptType()); | ||
} | ||
|
||
/** | ||
* Test script order with all params provided. | ||
* | ||
* @return void | ||
*/ | ||
public function testAdvancedScriptSortOrder() | ||
{ | ||
$scriptType = 'number'; | ||
$scriptLang = 'painless'; | ||
$scriptSource = "doc['sortField'].value * params.factor"; | ||
$scriptParams = ['factor' => 1.1]; | ||
$scriptDirection = SortOrderInterface::SORT_DESC; | ||
$scriptName = 'mySortOrder'; | ||
$scriptMissing = SortOrderInterface::MISSING_FIRST; | ||
|
||
$scriptSorOrder = new ScriptSortOrder( | ||
$scriptType, | ||
$scriptLang, | ||
$scriptSource, | ||
$scriptParams, | ||
$scriptDirection, | ||
$scriptName, | ||
$scriptMissing | ||
); | ||
|
||
// Default values. | ||
$this->assertEquals($scriptName, $scriptSorOrder->getName()); | ||
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField()); | ||
$this->assertEquals(SortOrderInterface::SORT_DESC, $scriptSorOrder->getDirection()); | ||
$this->assertEquals(ScriptSortOrder::TYPE_SCRIPT, $scriptSorOrder->getType()); | ||
$this->assertEquals(SortOrderInterface::MISSING_FIRST, $scriptSorOrder->getMissing()); | ||
|
||
$this->assertEquals( | ||
[ | ||
'lang' => $scriptLang, | ||
'source' => $scriptSource, | ||
'params' => $scriptParams, | ||
], | ||
$scriptSorOrder->getScript(), | ||
); | ||
$this->assertEquals($scriptType, $scriptSorOrder->getScriptType()); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/module-elasticsuite-thesaurus/Config/ThesaurusCacheConfig.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade this module to newer versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard BAYET <richard.bayet@smile.fr> | ||
* @copyright 2024 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteThesaurus\Config; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface; | ||
|
||
/** | ||
* Thesaurus cache configuration helper. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteThesaurus | ||
* @author Richard Bayet <richard.bayet@smile.fr> | ||
*/ | ||
class ThesaurusCacheConfig | ||
{ | ||
/** @var string */ | ||
const ALWAYS_CACHE_RESULTS_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/always'; | ||
|
||
/** @var string */ | ||
const MIN_REWRITES_FOR_CACHING_XML_PATH = 'smile_elasticsuite_thesaurus_settings/cache/min_rewites'; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ScopeConfigInterface $scopeConfig Scope config interface. | ||
*/ | ||
public function __construct(ScopeConfigInterface $scopeConfig) | ||
{ | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Returns true if it is allowed by the config to store in cache the results of the thesaurus rules computation. | ||
* | ||
* @param ContainerConfigurationInterface $config Container configuration. | ||
* @param int $rewritesCount Number of rewrites/alternative queries. | ||
* | ||
* @return bool | ||
*/ | ||
public function isCacheStorageAllowed(ContainerConfigurationInterface $config, $rewritesCount) | ||
{ | ||
$alwaysCache = $this->scopeConfig->isSetFlag( | ||
self::ALWAYS_CACHE_RESULTS_XML_PATH, | ||
ScopeInterface::SCOPE_STORES, | ||
$config->getStoreId() | ||
); | ||
|
||
if (false === $alwaysCache) { | ||
$minRewritesForCaching = $this->scopeConfig->getValue( | ||
self::MIN_REWRITES_FOR_CACHING_XML_PATH, | ||
ScopeInterface::SCOPE_STORES, | ||
$config->getStoreId() | ||
); | ||
|
||
return ($rewritesCount >= $minRewritesForCaching); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.