Skip to content

Commit

Permalink
Merge branch '2.11.x' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Jul 12, 2024
2 parents 85ea551 + b1bc7f4 commit a4adea5
Show file tree
Hide file tree
Showing 37 changed files with 771 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function getFunction(ContainerConfigurationInterface $containerConfigurat
{
$field = $this->getField($containerConfiguration, $optimizer);
$scaleFactor = (float) $optimizer->getConfig('scale_factor');
$queryName = sprintf('Optimizer [%s]:%d', $optimizer->getName(), $optimizer->getId());
$query = $optimizer->getRuleCondition()->getSearchQuery();
$query->setName(($query->getName() !== '') ? $queryName . " => " . $query->getName() : $queryName);

$function = [
'field_value_factor' => [
Expand All @@ -65,7 +68,7 @@ public function getFunction(ContainerConfigurationInterface $containerConfigurat
'modifier' => $optimizer->getConfig('scale_function'),
'missing' => 1 / $scaleFactor,
],
'filter' => $optimizer->getRuleCondition()->getSearchQuery(),
'filter' => $query,
];

return $function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ class ConstantScore implements ApplierInterface
*/
public function getFunction(ContainerConfigurationInterface $containerConfiguration, OptimizerInterface $optimizer)
{
$queryName = sprintf('Optimizer [%s]:%d', $optimizer->getName(), $optimizer->getId());
$query = $optimizer->getRuleCondition()->getSearchQuery();
$query->setName(($query->getName() !== '') ? $queryName . " => " . $query->getName() : $queryName);

$function = [
'weight' => 1 + ((float) $optimizer->getConfig('constant_score_value') / 100),
'filter' => $optimizer->getRuleCondition()->getSearchQuery(),
'filter' => $query,
];

return $function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function getSearchQuery()
}
}

$queryParams['name'] = $this->asStringRecursive();
$query = $this->queryFactory->create(QueryInterface::TYPE_BOOL, $queryParams);

return $query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getSearchQuery(ProductCondition $productCondition)
$queryParams = ['field' => $this->getSearchFieldName($productCondition)];
}

$query = $this->prepareQuery($queryType, $queryParams);
$query = $this->prepareQuery($queryType, $queryParams)->setName($productCondition->asString());

if (substr($productCondition->getOperator(), 0, 1) === '!') {
$query = $this->applyNegation($query);
Expand All @@ -122,6 +122,7 @@ public function getSearchQuery(ProductCondition $productCondition)
}

$query = $this->queryFactory->create(QueryInterface::TYPE_NESTED, $nestedQueryParams);
$query->setName($productCondition->asString());
}
}

Expand Down Expand Up @@ -179,7 +180,11 @@ private function getMatchQueryParams(ProductCondition $productCondition)
$queryText = $productCondition->getValue();
$minimumShouldMatch = "100%";

return ['field' => $fieldName, 'queryText' => $queryText, 'minimumShouldMatch' => $minimumShouldMatch];
return [
'field' => $fieldName,
'queryText' => $queryText,
'minimumShouldMatch' => $minimumShouldMatch,
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public function getSearchQuery(ProductCondition $condition)
$bounds['gt'] = $dateReference->format('Y-m-d');
break;
case '>=':
$bounds['gte'] = $dateReference->format('Y-m-d');
$bounds['lte'] = $dateReference->format('Y-m-d');
break;
case '<=':
$bounds['lte'] = $dateReference->format('Y-m-d');
$bounds['gte'] = $dateReference->format('Y-m-d');
break;
default:
throw new \InvalidArgumentException('Invalid operator');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ function (\Magento\Framework\Api\Search\Document $doc) {
$docIds[] = 0;
}

$this->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
$this->getSelect()->where('e.entity_id IN (?)', ['in' => $docIds]);
$orderList = join(',', $docIds);
$this->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface FieldInterface
const FIELD_TYPE_NESTED = 'nested';
const FIELD_TYPE_OBJECT = 'object';
const FIELD_TYPE_KNN_VECTOR = 'knn_vector';
const FIELD_TYPE_TOKEN_COUNT = 'token_count';

/**
* Analyzers declarations.
Expand Down
12 changes: 12 additions & 0 deletions src/module-elasticsuite-core/Index/Mapping/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ public function getMappingPropertyConfig(): array
}
}

if ($this->getType() === self::FIELD_TYPE_TOKEN_COUNT) {
$property = $this->getPropertyConfig(
$this->getDefaultSearchAnalyzer() ?? self::ANALYZER_KEYWORD
);
}

return $property;
}

Expand Down Expand Up @@ -409,6 +415,7 @@ private function getFieldAnalyzers(): array
* @param string|null $analyzer Used analyzer.
*
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED): array
{
Expand Down Expand Up @@ -443,6 +450,11 @@ private function getPropertyConfig($analyzer = self::ANALYZER_UNTOUCHED): array
$fieldMapping['dimension'] = $this->config['dimension'];
$fieldMapping['method'] = $this->config['model'];
break;
case self::FIELD_TYPE_TOKEN_COUNT:
$fieldMapping['analyzer'] = $analyzer;
$fieldMapping['store'] = true;
$fieldMapping['enable_position_increments'] = false;
break;
}

return $fieldMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Search\Adapter\Elasticsuite\Request\Collapse;

use Smile\ElasticsuiteCore\Search\Request\CollapseInterface;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\InnerHits\Builder as InnerHitsBuilder;

/**
* Build the part of the ES request collapsing search results
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class Builder
{
/**
* @var InnerHitsBuilder
*/
private $innerHitsBuilder;

/**
* Builder constructor.
*
* @param InnerHitsBuilder $innerHitsBuilder Inner hits builder.
*/
public function __construct(InnerHitsBuilder $innerHitsBuilder)
{
$this->innerHitsBuilder = $innerHitsBuilder;
}

/**
* Build the ES collapse section of the request from a Collapse
*
* @param CollapseInterface $collapseConfig Collapse configuration
*
* @return array
*/
public function buildCollapse(CollapseInterface $collapseConfig)
{
$collapse = [
'field' => $collapseConfig->getField(),
];

$innerHits = [];
foreach ($collapseConfig->getInnerHits() as $innerHitsConfig) {
$innerHits[] = $this->innerHitsBuilder->buildInnerHits($innerHitsConfig);
}
if (!empty($innerHits)) {
$collapse['inner_hits'] = $innerHits;
}

return $collapse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Search\Adapter\Elasticsuite\Request\InnerHits;

use Smile\ElasticsuiteCore\Search\Request\InnerHitsInterface;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\SortOrder\Builder as SortOrderBuilder;

/**
* Query inner hits builder.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class Builder
{
/**
* @var SortOrderBuilder
*/
private $sortOrderBuilder;

/**
* Builder constructor.
*
* @param SortOrderBuilder $sortOrderBuilder Sort order builder.
*/
public function __construct(SortOrderBuilder $sortOrderBuilder)
{
$this->sortOrderBuilder = $sortOrderBuilder;
}

/**
* Build an inner hits definition into an ES inner hits section.
*
* @param InnerHitsInterface $innerHitsConfig Inner hits.
*
* @return array
*/
public function buildInnerHits(InnerHitsInterface $innerHitsConfig)
{
$innerHits = [
'name' => $innerHitsConfig->getName(),
'from' => $innerHitsConfig->getFrom(),
'size' => $innerHitsConfig->getSize(),
];

$sortOrders = $this->sortOrderBuilder->buildSortOrders($innerHitsConfig->getSort());
if (!empty($sortOrders)) {
$innerHits['sort'] = $sortOrders;
}

return $innerHits;
}
}
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\Adapter\Elasticsuite\Request\SortOrder\Builder as SortOrderBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\Builder as AggregationBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Collapse\Builder as CollapseBuilder;
use Smile\ElasticsuiteCore\Search\RequestInterface;

/**
Expand All @@ -43,21 +44,29 @@ class Mapper
*/
private $aggregationBuilder;

/**
* @var CollapseBuilder
*/
private $collapseBuilder;

/**
* Constructor.
*
* @param QueryBuilder $queryBuilder Adapter query builder.
* @param SortOrderBuilder $sortOrderBuilder Adapter sort orders builder.
* @param AggregationBuilder $aggregationBuilder Adapter aggregations builder.
* @param CollapseBuilder $collapseBuilder Adapter collapse builder.
*/
public function __construct(
QueryBuilder $queryBuilder,
SortOrderBuilder $sortOrderBuilder,
AggregationBuilder $aggregationBuilder
AggregationBuilder $aggregationBuilder,
CollapseBuilder $collapseBuilder
) {
$this->queryBuilder = $queryBuilder;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->aggregationBuilder = $aggregationBuilder;
$this->collapseBuilder = $collapseBuilder;
}

/**
Expand Down Expand Up @@ -99,6 +108,11 @@ public function buildSearchRequest(RequestInterface $request)
$searchRequest['min_score'] = $request->getMinScore();
}

$collapse = $this->getCollapse($request);
if (!empty($collapse)) {
$searchRequest['collapse'] = $collapse;
}

return $searchRequest;
}

Expand Down Expand Up @@ -167,4 +181,22 @@ private function getAggregations(RequestInterface $request)

return $aggregations;
}

/**
* Extract and build collapse configuration of the search request
*
* @param RequestInterface $request Search request.
*
* @return array
*/
private function getCollapse(RequestInterface $request)
{
$collapse = [];

if ($request->hasCollapse()) {
$collapse = $this->collapseBuilder->buildCollapse($request->getCollapse());
}

return $collapse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function buildQuery(QueryInterface $query)
$searchQueryParams['_name'] = $query->getName();
}

if ($query->getMinScore() > 0) {
$searchQueryParams['min_score'] = $query->getMinScore();
}

return ['function_score' => $searchQueryParams];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function buildQuery(QueryInterface $query)
'boost' => $query->getBoost(),
];

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

if ($query->getName()) {
$searchQuery['match']['_name'] = $query->getName();
$searchQueryParams['_name'] = $query->getName();
}

$searchQuery = ['match' => [$query->getField() => $searchQueryParams]];

return $searchQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function buildQuery(QueryInterface $query)
$searchQuery = ['range' => [$query->getField() => $queryParams]];

if ($query->getName()) {
$searchQuery['range']['_name'] = $query->getName();
// Intentional omission of the '_name' management because range query does not support it.
}

return $searchQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ public function buildQuery(QueryInterface $query)
throw new \InvalidArgumentException("Query builder : invalid query type {$query->getType()}");
}

return [
$searchQuery = [
'span_first' => [
'boost' => $query->getBoost(),
'match' => $this->parentBuilder->buildQuery($query->getMatch()),
'end' => $query->getEnd(),
],
];

if ($query->getName()) {
$searchQuery['span_first']['_name'] = $query->getName();
}

return $searchQuery;
}
}
Loading

0 comments on commit a4adea5

Please sign in to comment.