Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip: [CatalogOptimizer] Function scores filter prebuilding and caching #3319

Draft
wants to merge 1 commit into
base: 2.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Framework\App\Config\ScopeConfigInterface;
use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Functions\ProviderInterface;
use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Functions\Filter\PreBuilder as FunctionFilterPreBuilder;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
Expand All @@ -38,6 +39,11 @@ class ApplierList
*/
private $functionsProvider;

/**
* @var FunctionFilterPreBuilder
*/
private $preBuilder;

/**
* @var ScopeConfigInterface
*/
Expand All @@ -53,17 +59,20 @@ class ApplierList
*
* @param QueryFactory $queryFactory Query factory.
* @param ProviderInterface $functionsProvider Functions provider.
* @param FunctionFilterPreBuilder $preBuilder Function filter pre-builder.
* @param ScopeConfigInterface $scopeConfig Scope configuration.
* @param OptimizerFilterInterface[] $filters Optimizer filters.
*/
public function __construct(
QueryFactory $queryFactory,
ProviderInterface $functionsProvider,
FunctionFilterPreBuilder $preBuilder,
ScopeConfigInterface $scopeConfig,
array $filters = []
) {
$this->queryFactory = $queryFactory;
$this->functionsProvider = $functionsProvider;
$this->preBuilder = $preBuilder;
$this->scopeConfig = $scopeConfig;
$this->filters = $filters;
}
Expand All @@ -85,6 +94,8 @@ public function applyOptimizers(ContainerConfigurationInterface $containerConfig
$functions = array_intersect_key($functions, array_flip($optimizerIds));
}

$functions = $this->preBuilder->prebuild($containerConfiguration, $functions);

return $this->applyFunctions($query, $functions);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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 <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Functions\Filter;

use Magento\Framework\App\CacheInterface;
use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* PreBuilder of function score queries.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogOptimizer
*/
class PreBuilder
{
/** @var int default lifetime for computed optimizers functions. */
const DEFAULT_CACHE_LIFETIME = 7200;

/** @var Builder */
private $builder;

/** @var CacheInterface */
private $cache;

/** @var integer|mixed */
private $cacheLifeTime;

/** @var array */
private $prebuiltFilters;

/**
* Constructor.
*
* @param Builder $builder Adapter request builder.
* @param CacheInterface $cache Cache manager.
* @param int $cacheLifetime Cache Lifetime of prebuilt optimizers function filters.
*/
public function __construct(
Builder $builder,
CacheInterface $cache,
$cacheLifetime = self::DEFAULT_CACHE_LIFETIME
) {
$this->builder = $builder;
$this->cache = $cache;
$this->cacheLifeTime = $cacheLifetime;
}

/**
* Prebuild function queries of optimizers and apply cache.
*
* @param ContainerConfigurationInterface $containerConfiguration Container configuration.
* @param array $functions Functions whose queries to prebuild.
*
* @return array
*/
public function prebuild(ContainerConfigurationInterface $containerConfiguration, $functions)
{
// Test if enabled.
foreach ($functions as $optimizerId => &$function) {
if (isset($function['filter'])) {
if ($function['filter'] instanceof QueryInterface) {
$function['filter'] = $this->prebuildFilter(
$containerConfiguration,
$optimizerId,
$function['filter']
);
}
}
}

return $functions;
}

/**
* Prebuild an optimizer function filter query into a portion of Elasticsearch request.
*
* @param ContainerConfigurationInterface $containerConfiguration Container configuration.
* @param int $optimizerId Optimizer id.
* @param QueryInterface $filter Filter query.
*
* @return array
*/
private function prebuildFilter(ContainerConfigurationInterface $containerConfiguration, $optimizerId, QueryInterface $filter)
{
$cacheKey = sprintf(
"%s_%s_%s",
'optimizer_boost_function_prebuild',
$containerConfiguration->getStoreId(),
$optimizerId
);

if (!isset($this->prebuiltFilters[$cacheKey])) {
if ($prebuiltFilter = $this->cache->load($cacheKey)) {
$prebuiltFilter = unserialize($prebuiltFilter);
} else {
$prebuiltFilter = $this->builder->buildQuery($filter);
$this->cache->save(serialize($prebuiltFilter), $cacheKey, [Optimizer::CACHE_TAG], $this->cacheLifeTime);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append the Elasticsuite global tag

}

$this->prebuiltFilters[$cacheKey] = $prebuiltFilter;
}

return $this->prebuiltFilters[$cacheKey];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function buildQuery(QueryInterface $query)

foreach ($searchQueryParams['functions'] as &$function) {
if (isset($function['filter'])) {
$function['filter'] = $this->parentBuilder->buildQuery($function['filter']);
// Filter query might have been prebuilt for performance reasons, if not built it now.
if ($function['filter'] instanceof QueryInterface) {
$function['filter'] = $this->parentBuilder->buildQuery($function['filter']);
}
}
}

Expand Down
Loading