-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
386 additions
and
31 deletions.
There are no files selected for viewing
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,168 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearchElastic\Adapter\Aggregation; | ||
|
||
use Algolia\AlgoliaSearch\Model\Indexer\Product; | ||
use Algolia\AlgoliaSearchElastic\Helper\ElasticAdapterHelper; | ||
use Magento\Catalog\Model\ProductFactory; | ||
use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder as ElasticSearchBuilder; | ||
use Magento\Elasticsearch\SearchAdapter\Aggregation\Builder\BucketBuilderInterface; | ||
use Magento\Elasticsearch\SearchAdapter\Aggregation\DataProviderFactory; | ||
use Magento\Elasticsearch\SearchAdapter\QueryContainer; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\Search\Dynamic\DataProviderInterface; | ||
use Magento\Framework\Search\RequestInterface; | ||
|
||
class Builder extends ElasticSearchBuilder | ||
{ | ||
/** | ||
* @var DataProviderFactory | ||
*/ | ||
private $dataProviderFactory; | ||
|
||
/** | ||
* @var QueryContainer | ||
*/ | ||
private $query = null; | ||
|
||
/** | ||
* @var ProductFactory | ||
*/ | ||
private $productFactory; | ||
|
||
/** | ||
* @var \Magento\Catalog\Model\Product | ||
*/ | ||
private $product; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $facets = []; | ||
|
||
/** | ||
* @param array $dataProviderContainer | ||
* @param array $aggregationContainer | ||
* @param DataProviderFactory|null $dataProviderFactory | ||
* @param ProductFactory $productFactory | ||
*/ | ||
public function __construct( | ||
array $dataProviderContainer, | ||
array $aggregationContainer, | ||
DataProviderFactory $dataProviderFactory = null, | ||
ProductFactory $productFactory | ||
) { | ||
$this->dataProviderContainer = array_map( | ||
function (DataProviderInterface $dataProvider) { | ||
return $dataProvider; | ||
}, | ||
$dataProviderContainer | ||
); | ||
$this->aggregationContainer = array_map( | ||
function (BucketBuilderInterface $bucketBuilder) { | ||
return $bucketBuilder; | ||
}, | ||
$aggregationContainer | ||
); | ||
$this->dataProviderFactory = $dataProviderFactory | ||
?: ObjectManager::getInstance()->get(DataProviderFactory::class); | ||
|
||
$this->productFactory = $productFactory; | ||
} | ||
|
||
public function build(RequestInterface $request, array $queryResult) | ||
{ | ||
$aggregations = []; | ||
$buckets = $request->getAggregation(); | ||
|
||
$facets = $this->getFacets(); | ||
|
||
$dataProvider = $this->dataProviderFactory->create( | ||
$this->dataProviderContainer[$request->getIndex()], | ||
$this->query | ||
); | ||
|
||
foreach ($buckets as $bucket) { | ||
if (count($facets) && isset($facets[$bucket->getField()])) { | ||
$aggregations[$bucket->getName()] = | ||
$this->formatAggregation($bucket->getField(), $facets[$bucket->getField()]); | ||
} else { | ||
$bucketAggregationBuilder = $this->aggregationContainer[$bucket->getType()]; | ||
$aggregations[$bucket->getName()] = $bucketAggregationBuilder->build( | ||
$bucket, | ||
$request->getDimensions(), | ||
$queryResult, | ||
$dataProvider | ||
); | ||
} | ||
} | ||
|
||
$this->query = null; | ||
|
||
return $aggregations; | ||
} | ||
|
||
|
||
private function formatAggregation($attribute, $facetData) | ||
{ | ||
$aggregation = []; | ||
|
||
foreach ($facetData as $value => $count) { | ||
$optionId = $this->getOptionIdByLabel($attribute, $value); | ||
$aggregation[$optionId] = [ | ||
'value' => (string) $optionId, | ||
'count' => (string) $count, | ||
]; | ||
} | ||
|
||
return $aggregation; | ||
} | ||
|
||
private function getOptionIdByLabel($attributeCode, $optionLabel) | ||
{ | ||
$product = $this->getProduct(); | ||
$isAttributeExist = $product->getResource()->getAttribute($attributeCode); | ||
$optionId = ''; | ||
if ($isAttributeExist && $isAttributeExist->usesSource()) { | ||
$optionId = $isAttributeExist->getSource()->getOptionId($optionLabel); | ||
} | ||
|
||
return $optionId; | ||
} | ||
|
||
/** | ||
* @return \Magento\Catalog\Model\Product | ||
*/ | ||
private function getProduct() | ||
{ | ||
if (!$this->product) { | ||
$this->product = $this->productFactory->create(); | ||
} | ||
|
||
return $this->product; | ||
} | ||
|
||
/** | ||
* Sets the QueryContainer instance to the internal property in order to use it in build process | ||
* | ||
* @param QueryContainer $query | ||
* @return \Magento\Elasticsearch\SearchAdapter\Aggregation\Builder | ||
*/ | ||
public function setQuery(QueryContainer $query) | ||
{ | ||
$this->query = $query; | ||
|
||
return $this; | ||
} | ||
|
||
public function setFacets($facets) | ||
{ | ||
$this->facets = $facets; | ||
} | ||
|
||
private function getFacets() | ||
{ | ||
return $this->facets; | ||
} | ||
|
||
} |
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,7 @@ | ||
# CHANGE LOG | ||
|
||
## 0.0.1 | ||
|
||
### UPDATES | ||
- Updated composer.json (#4) (by @vmalyk) | ||
- Fix Bad Request Warning and Builder Aggregation (#6) |
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 | ||
|
||
namespace Algolia\AlgoliaSearchElastic\Helper; | ||
|
||
use Algolia\AlgoliaSearch\Helper\Adapter\FiltersHelper; | ||
use Algolia\AlgoliaSearch\Helper\AdapterHelper; | ||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
use Algolia\AlgoliaSearch\Helper\Data as AlgoliaDataHelper; | ||
use Magento\CatalogSearch\Helper\Data as CatalogSearchDataHelper; | ||
|
||
class ElasticAdapterHelper | ||
{ | ||
/** | ||
* @var AdapterHelper | ||
*/ | ||
private $adapterHelper; | ||
|
||
/** | ||
* @var ConfigHelper | ||
*/ | ||
private $configHelper; | ||
|
||
/** | ||
* @param AdapterHelper $adapterHelper | ||
* @param ConfigHelper $configHelper | ||
*/ | ||
public function __construct( | ||
AdapterHelper $adapterHelper, | ||
ConfigHelper $configHelper | ||
) { | ||
$this->adapterHelper = $adapterHelper; | ||
$this->configHelper = $configHelper; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function replaceElasticSearchResults() | ||
{ | ||
if (!$this->adapterHelper->isAllowed() | ||
|| !( | ||
$this->adapterHelper->isSearch() || | ||
$this->adapterHelper->isReplaceCategory() || | ||
$this->adapterHelper->isReplaceAdvancedSearch() || | ||
$this->adapterHelper->isLandingPage() | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getStoreId() | ||
{ | ||
return $this->configHelper->getStoreId(); | ||
} | ||
|
||
public function getFacets() | ||
{ | ||
return $this->configHelper->getFacets($this->getStoreId()); | ||
} | ||
|
||
} |
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,108 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearchElastic\Plugin; | ||
|
||
use Algolia\AlgoliaSearch\Helper\AdapterHelper; | ||
use Algolia\AlgoliaSearchElastic\Helper\ElasticAdapterHelper; | ||
use Magento\Catalog\Model\ProductFactory; | ||
|
||
class FulltextCollection | ||
{ | ||
/** @var AdapterHelper */ | ||
private $adapterHelper; | ||
|
||
/** @var ElasticAdapterHelper */ | ||
private $esAdapterHelper; | ||
|
||
/** @var ProductFactory */ | ||
private $productFactory; | ||
|
||
/** @var \Magento\Catalog\Model\Product */ | ||
private $product; | ||
|
||
private $facets; | ||
|
||
/** | ||
* @param AdapterHelper $adapterHelper | ||
*/ | ||
public function __construct( | ||
AdapterHelper $adapterHelper, | ||
ElasticAdapterHelper $esAdapterHelper, | ||
ProductFactory $productFactory | ||
) { | ||
$this->adapterHelper = $adapterHelper; | ||
$this->esAdapterHelper = $esAdapterHelper; | ||
$this->productFactory = $productFactory; | ||
} | ||
|
||
/** | ||
* @param \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject | ||
* @param $field | ||
* @param null $condition | ||
*/ | ||
public function beforeAddFieldToFilter( | ||
\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject, | ||
$field, | ||
$condition = null | ||
) { | ||
if (!$condition || !$this->esAdapterHelper->replaceElasticSearchResults()) { | ||
return [$field, $condition]; | ||
} | ||
|
||
if (is_array($this->getFacets()) && in_array($field, $this->getFacets())) { | ||
$condition = $this->getOptionIdByLabel($field, $condition); | ||
} | ||
|
||
return [$field, $condition]; | ||
} | ||
|
||
/** | ||
* @param string $attributeCode | ||
* @param null $optionLabel | ||
* @return string | ||
*/ | ||
private function getOptionIdByLabel($attributeCode, $optionLabel = null) | ||
{ | ||
if ($optionLabel && !is_array($optionLabel)) { | ||
$product = $this->getProduct(); | ||
$isAttributeExist = $product->getResource()->getAttribute($attributeCode); | ||
if ($isAttributeExist && $isAttributeExist->usesSource()) { | ||
$optionLabel = $isAttributeExist->getSource()->getOptionId($optionLabel); | ||
} | ||
} | ||
|
||
return $optionLabel; | ||
} | ||
|
||
/** | ||
* @return \Magento\Catalog\Model\Product | ||
*/ | ||
public function getProduct() | ||
{ | ||
if (!$this->product) { | ||
$this->product = $this->productFactory->create(); | ||
} | ||
|
||
return $this->product; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getFacets() | ||
{ | ||
if (!$this->facets) { | ||
$facets = []; | ||
$configFacets = $this->esAdapterHelper->getFacets(); | ||
if (is_array($configFacets) && count($configFacets)) { | ||
$facets = array_map(function ($facet) { | ||
return $facet['attribute']; | ||
}, $configFacets); | ||
} | ||
$this->facets = $facets; | ||
} | ||
|
||
return $this->facets; | ||
} | ||
|
||
} |
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