Skip to content

Commit

Permalink
Refactoring aggregation to be compliant with Elasticsuite 2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed May 9, 2019
1 parent 29780ee commit 9b09810
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 35 deletions.
40 changes: 11 additions & 29 deletions Model/Layer/Filter/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @copyright 2017 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteRating\Model\Layer\Filter;

/**
Expand All @@ -26,25 +27,6 @@ class Rating extends \Smile\ElasticsuiteCatalog\Model\Layer\Filter\Attribute
*/
const RATING_AGG_INTERVAL = 20;

/**
* {@inheritDoc}
*/
public function addFacetToCollection($config = [])
{
$facetConfig = [
'name' => $this->getFilterField(),
'type' => \Smile\ElasticsuiteCore\Search\Request\BucketInterface::TYPE_HISTOGRAM,
'minDocCount' => 1,
'interval' => (int) self::RATING_AGG_INTERVAL
];

/** @var \Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection $productCollection */
$productCollection = $this->getLayer()->getProductCollection();
$productCollection->addFacet($facetConfig);

return $this;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -123,18 +105,18 @@ protected function _getItemsData()
}
}
krsort($optionsFacetedData);

$minCount = !empty($optionsFacetedData) ? min(array_column($optionsFacetedData, 'count')) : 0;

if (!empty($this->currentFilterValue) || $minCount < $productCollection->getSize()) {
foreach ($optionsFacetedData as $value => $data) {
$sumCount += (int) $data['count'];
$items[$value] = [
'label' => $value,
'value' => $value,
'count' => $sumCount,
];
}
if (!empty($this->currentFilterValue) || $minCount < $productCollection->getSize()) {
foreach ($optionsFacetedData as $value => $data) {
$sumCount += (int) $data['count'];
$items[$value] = [
'label' => $value,
'value' => $value,
'count' => $sumCount,
];
}
}

return $items;
Expand Down
66 changes: 66 additions & 0 deletions Plugin/Search/Request/Product/Attribute/AggregationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteRating
* @author Romain Ruaud <[email protected]>
* @copyright 2019 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteRating\Plugin\Search\Request\Product\Attribute;

/**
* Plugin to set aggregation builder for ratings.
*
* @category Smile
* @package Smile\ElasticsuiteRating
* @author Romain Ruaud <[email protected]>
*/
class AggregationResolver
{
/**
* Rating Summary attribute code.
*/
const RATING_SUMMARY_ATTRIBUTE = 'ratings_summary';

/**
* @var \Smile\ElasticsuiteRating\Search\Request\Product\Attribute\Aggregation\Rating
*/
private $ratingAggregation;

/**
* AggregationResolver constructor.
*
* @param \Smile\ElasticsuiteRating\Search\Request\Product\Attribute\Aggregation\Rating $ratingAggregation Rating Aggregation
*/
public function __construct(\Smile\ElasticsuiteRating\Search\Request\Product\Attribute\Aggregation\Rating $ratingAggregation)
{
$this->ratingAggregation = $ratingAggregation;
}

/**
* Set default facet size to 0 for swatches attributes before adding it as aggregation.
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param \Smile\ElasticsuiteCatalog\Search\Request\Product\Attribute\AggregationResolver $subject Aggregation Resolver
* @param array $result Aggregation Config
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute Attribute
*
* @return array
*/
public function afterGetAggregationData(
\Smile\ElasticsuiteCatalog\Search\Request\Product\Attribute\AggregationResolver $subject,
$result,
$attribute
) {
if ($attribute->getAttributeCode() === self::RATING_SUMMARY_ATTRIBUTE) {
$result = $this->ratingAggregation->getAggregationData($attribute);
}

return $result;
}
}
62 changes: 62 additions & 0 deletions Search/Request/Product/Attribute/Aggregation/Rating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteRating
* @author Romain Ruaud <[email protected]>
* @copyright 2019 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteRating\Search\Request\Product\Attribute\Aggregation;

use Smile\ElasticsuiteCatalog\Search\Request\Product\Attribute\AggregationInterface;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;

/**
* Aggregation builder for product ratings.
*
* @category Smile
* @package Smile\ElasticsuiteRating
* @author Romain Ruaud <[email protected]>
*/
class Rating implements AggregationInterface
{
/**
* Default interval, based on 0-100 divided in five stars.
*/
const RATING_AGG_INTERVAL = 20;

/**
* {@inheritdoc}
*/
public function getAggregationData(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute)
{
$bucketConfig = [
'name' => $this->getFilterField($attribute),
'type' => BucketInterface::TYPE_HISTOGRAM,
'minDocCount' => 1,
'interval' => (int) self::RATING_AGG_INTERVAL,
];

return $bucketConfig;
}

/**
* Retrieve ES filter field.
*
* @param \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute Attribute
*
* @return string
*/
private function getFilterField(\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute)
{
$field = $attribute->getAttributeCode();

return $field;
}

}
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"description" : "Smile ElasticSuite - Rating search module.",
"homepage" : "https://github.com/Smile-SA/module-elasticsuite-catalog",
"homepage" : "https://github.com/Smile-SA/magento2-module-elasticsuite-rating",
"keywords" : [
"magento",
"magento2",
Expand All @@ -29,10 +29,10 @@
}
],
"require" : {
"magento/framework" : ">=100.1.0",
"magento/framework" : ">=102.0.0",
"magento/magento-composer-installer" : "*",
"magento/module-review" : ">=100.1.0",
"smile/elasticsuite" : "~2.7.0"
"smile/elasticsuite" : "~2.8.0"
},
"require-dev" : {
"smile/magento2-smilelab-quality-suite" : "1.0.0"
Expand Down
35 changes: 35 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<!--
/**
* Smile_ElasticsuiteCatalog dependency injection configuration.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteRating
* @author Romain Ruaud <[email protected]>
* @copyright 2019 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<!-- Datasources resolver -->
<type name="Smile\ElasticsuiteCore\Index\DataSourceResolver">
<arguments>
<argument name="datasources" xsi:type="array">
<item name="catalog_product" xsi:type="array">
<item name="rating" xsi:type="object">Smile\ElasticsuiteRating\Model\Product\Indexer\Fulltext\Datasource\RatingData</item>
</item>
</argument>
</arguments>
</type>

<type name="\Smile\ElasticsuiteCatalog\Search\Request\Product\Attribute\AggregationResolver">
<plugin name="rating_aggregation" type="Smile\ElasticsuiteRating\Plugin\Search\Request\Product\Attribute\AggregationResolver"/>
</type>

</config>
3 changes: 0 additions & 3 deletions etc/elasticsuite_indices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
xsi:noNamespaceSchemaLocation="urn:magento:module:Smile_ElasticsuiteCore:etc/elasticsuite_indices.xsd">
<index identifier="catalog_product" defaultSearchType="product">
<type name="product" idFieldName="entity_id">
<datasources>
<datasource name="rating">Smile\ElasticsuiteRating\Model\Product\Indexer\Fulltext\Datasource\RatingData</datasource>
</datasources>
<mapping>
<field name="ratings_summary" type="double"/>
</mapping>
Expand Down

0 comments on commit 9b09810

Please sign in to comment.