Skip to content

Commit

Permalink
Merge branch '2.10.x' into 2.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Nov 27, 2024
2 parents 7fb60e0 + 3018a00 commit 5bc451c
Show file tree
Hide file tree
Showing 12 changed files with 2,403 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,27 @@ class Ajax extends \Magento\Framework\App\Action\Action
*/
private $categoryRepository;

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* Constructor.
*
* @param \Magento\Framework\App\Action\Context $context Controller action context.
* @param \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory JSON result factory.
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver Layer resolver.
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository Category Repository.
* @param \Psr\Log\LoggerInterface $logger Logger.
* @param \Magento\Catalog\Model\Layer\FilterList[] $filterListPool Filter list pool.
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Psr\Log\LoggerInterface $logger,
$filterListPool = []
) {
parent::__construct($context);
Expand All @@ -69,6 +76,7 @@ public function __construct(
$this->layerResolver = $layerResolver;
$this->filterListPool = $filterListPool;
$this->categoryRepository = $categoryRepository;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -101,12 +109,16 @@ private function initLayer()
$this->layerResolver->create($this->getLayerType());

if ($this->getRequest()->getParam('cat')) {
$category = $this->categoryRepository->get(
$this->getRequest()->getParam('cat'),
$this->layerResolver->get()->getCurrentStore()->getId()
);

$this->layerResolver->get()->setCurrentCategory($category);
try {
$category = $this->categoryRepository->get(
$this->getRequest()->getParam('cat'),
$this->layerResolver->get()->getCurrentStore()->getId()
);

$this->layerResolver->get()->setCurrentCategory($category);
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
$this->logger->critical($exception->getMessage());
}
}

$this->applyFilters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder as QueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Standard as StandardSortOrder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Nested as NestedSortOrder;
use Smile\ElasticsuiteCore\Search\Request\SortOrder\Script as ScriptSortOrder;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;

Expand Down Expand Up @@ -138,6 +139,50 @@ public function testNestedFilterSortOrder()
$this->assertEquals('query', $currentSortOrder['parent.child']['nested']['filter']);
}

/**
* Test building a script sort order (without support for direction yet).
*
* @return void
*/
public function testScriptSortOrder()
{
$scriptType = 'number';
$scriptLang = 'painless';
$scriptSource = "doc['sortField'].value * params.factor";
$scriptParams = ['factor' => 1.1];
$scriptSorOrder = new ScriptSortOrder(
$scriptType,
$scriptLang,
$scriptSource,
$scriptParams
);
$this->assertEquals(ScriptSortOrder::SCRIPT_FIELD, $scriptSorOrder->getField());
$this->assertEquals(
[
'lang' => $scriptLang,
'source' => $scriptSource,
'params' => $scriptParams,
],
$scriptSorOrder->getScript()
);

$sortOrders = $this->getSortOrderBuilder()->buildSortOrders([$scriptSorOrder]);

$currentSortOrder = current($sortOrders);
$this->assertIsArray($currentSortOrder);
$this->assertArrayHasKey($scriptSorOrder->getField(), $currentSortOrder);
$this->assertArrayHasKey('type', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
$this->assertEquals($scriptSorOrder->getScriptType(), $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['type']);
$this->assertArrayHasKey('script', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
$this->assertIsArray($currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['script']);
$this->assertEquals(
$scriptSorOrder->getScript(),
$currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]['script']
);
// 'order' not supported yet.
$this->assertArrayNotHasKey('order', $currentSortOrder[ScriptSortOrder::SCRIPT_FIELD]);
}

/**
* Init the sort order builder used in tests.
*
Expand Down
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 src/module-elasticsuite-thesaurus/Config/ThesaurusCacheConfig.php
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;
}
}
15 changes: 13 additions & 2 deletions src/module-elasticsuite-thesaurus/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusConfigFactory;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusConfig;
use Smile\ElasticsuiteThesaurus\Config\ThesaurusCacheConfig;
use Smile\ElasticsuiteThesaurus\Api\Data\ThesaurusInterface;
use Smile\ElasticsuiteCore\Helper\Cache as CacheHelper;

Expand Down Expand Up @@ -66,24 +67,32 @@ class Index
*/
private $cacheHelper;

/**
* @var ThesaurusConfig
*/
private $thesaurusCacheConfig;

/**
* Constructor.
*
* @param ClientInterface $client ES client.
* @param IndexSettingsHelper $indexSettingsHelper Index Settings Helper.
* @param CacheHelper $cacheHelper ES caching helper.
* @param ThesaurusConfigFactory $thesaurusConfigFactory Thesaurus configuration factory.
* @param ThesaurusCacheConfig $thesaurusCacheConfig Thesaurus cache configuration helper.
*/
public function __construct(
ClientInterface $client,
IndexSettingsHelper $indexSettingsHelper,
CacheHelper $cacheHelper,
ThesaurusConfigFactory $thesaurusConfigFactory
ThesaurusConfigFactory $thesaurusConfigFactory,
ThesaurusCacheConfig $thesaurusCacheConfig
) {
$this->client = $client;
$this->indexSettingsHelper = $indexSettingsHelper;
$this->thesaurusConfigFactory = $thesaurusConfigFactory;
$this->cacheHelper = $cacheHelper;
$this->thesaurusCacheConfig = $thesaurusCacheConfig;
}

/**
Expand All @@ -104,7 +113,9 @@ public function getQueryRewrites(ContainerConfigurationInterface $containerConfi

if ($queryRewrites === false) {
$queryRewrites = $this->computeQueryRewrites($containerConfig, $queryText, $originalBoost);
$this->cacheHelper->saveCache($cacheKey, $queryRewrites, $cacheTags);
if ($this->thesaurusCacheConfig->isCacheStorageAllowed($containerConfig, count($queryRewrites))) {
$this->cacheHelper->saveCache($cacheKey, $queryRewrites, $cacheTags);
}
}

return $queryRewrites;
Expand Down
Loading

0 comments on commit 5bc451c

Please sign in to comment.