-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3310 from rbayet/feat-support-collapse-inner-hits…
…-2.10.x [Core] Basic support for search results collapse and inner hits
- Loading branch information
Showing
11 changed files
with
509 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/module-elasticsuite-core/Search/Adapter/Elasticsuite/Request/Collapse/Builder.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,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; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/module-elasticsuite-core/Search/Adapter/Elasticsuite/Request/InnerHits/Builder.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,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; | ||
} | ||
} |
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
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,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
36
src/module-elasticsuite-core/Search/Request/CollapseInterface.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,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(); | ||
} |
Oops, something went wrong.