Skip to content

Commit

Permalink
Merge pull request #3310 from rbayet/feat-support-collapse-inner-hits…
Browse files Browse the repository at this point in the history
…-2.10.x

[Core] Basic support for search results collapse and inner hits
  • Loading branch information
rbayet authored Jul 2, 2024
2 parents 65b9e22 + 7c1abbc commit 3067317
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 2 deletions.
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 <[email protected]>
* @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 <[email protected]>
* @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;
}
}
32 changes: 32 additions & 0 deletions src/module-elasticsuite-core/Search/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Framework\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Api\Search\SpellcheckerInterface;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
use Smile\ElasticsuiteCore\Search\Request\CollapseInterface;
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;

/**
Expand All @@ -39,6 +40,11 @@ class Request extends \Magento\Framework\Search\Request implements RequestInterf
*/
private $filter;

/**
* @var CollapseInterface
*/
private $collapse;

/**
* @var integer
*/
Expand Down Expand Up @@ -148,6 +154,32 @@ public function isSpellchecked()
return in_array($this->spellingType, $fuzzySpellingTypes);
}

/**
* {@inheritDoc}
*/
public function setCollapse(CollapseInterface $collapse)
{
$this->collapse = $collapse;

return $this;
}

/**
* {@inheritDoc}
*/
public function hasCollapse()
{
return ($this->collapse instanceof CollapseInterface);
}

/**
* {@inheritDoc}
*/
public function getCollapse()
{
return $this->collapse;
}

/**
* Parse the track_total_hits directive to appropriate type : either int or bool.
* It's actually passed as a string when coming from the configuration file reader.
Expand Down
61 changes: 61 additions & 0 deletions src/module-elasticsuite-core/Search/Request/Collapse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\ElasticsuiteCore\Search\Request;

/**
* Search request collapse implementation.
*
* @category Smile
* @package Smile\ElasticsuiteCore
*/
class Collapse implements CollapseInterface
{
/**
* @var string
*/
private $field;

/**
* @var InnerHitsInterface[]
*/
private $innerHits;

/**
* Collapse constructor.
*
* @param string $field Field to collapse results on.
* @param InnerHitsInterface[] $innerHits Inner hits.
*/
public function __construct($field, $innerHits = [])
{
$this->field = $field;
$this->innerHits = $innerHits;
}

/**
* {@inheritDoc}
*/
public function getField()
{
return $this->field;
}

/**
* {@inheritDoc}
*/
public function getInnerHits()
{
return $this->innerHits;
}
}
36 changes: 36 additions & 0 deletions src/module-elasticsuite-core/Search/Request/CollapseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\ElasticsuiteCore\Search\Request;

/**
* Interface for the collapsing of search results.
*
* @package Smile\ElasticsuiteCore\Search\Request
*/
interface CollapseInterface
{
/**
* Return the field used to collapse search results.
*
* @return string
*/
public function getField();

/**
* Return the inner hits configurations, if any.
*
* @return InnerHitsInterface[]
*/
public function getInnerHits();
}
Loading

0 comments on commit 3067317

Please sign in to comment.