Skip to content

Commit

Permalink
Allow search request field mapping to be configured from di.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Sandu authored and botisSmile committed Oct 5, 2021
1 parent 08c8fa8 commit acb79b5
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 77 deletions.
173 changes: 173 additions & 0 deletions src/module-elasticsuite-catalog-graph-ql/etc/graphql/di.xml.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?xml version="1.0"?>
<!--
/**
* 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\ElasticsuiteCatalogGraphQl
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2020 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">
<preference for="Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder"
type="Smile\ElasticsuiteCatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder"/>
<preference for="Magento\CatalogGraphQl\Model\Resolver\Products" type="Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Products" />
<preference for="Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface" type="Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Products\Query\Search"/>

<virtualType name="elasticsuiteSearchResultApplier" type="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierFactory">
<arguments>
<argument name="instanceName" xsi:type="string">Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection\SearchResultApplier</argument>
</arguments>
</virtualType>

<type name="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch">
<arguments>
<argument name="searchResultsApplierFactory" xsi:type="object">elasticsuiteSearchResultApplier</argument>
</arguments>
</type>

<!-- 'aggregations' Data Providers -->
<preference for="Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Attribute"
type="Smile\ElasticsuiteCatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Attribute"/>
<preference for="Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Category"
type="Smile\ElasticsuiteCatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Category"/>
<preference for="Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Price"
type="Smile\ElasticsuiteCatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Price"/>


<!-- Layer filters implementation -->
<virtualType name="Smile\ElasticsuiteCatalogGraphQl\Model\ResourceModel\Product\SearchResultCollectionFactory"
type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
<arguments>
<argument name="instanceName" xsi:type="string">Smile\ElasticsuiteCatalogGraphQl\Model\ResourceModel\Product\SearchResultCollection</argument>
</arguments>
</virtualType>

<type name="Smile\ElasticsuiteCatalogGraphQl\Model\Layer\CollectionProvider">
<arguments>
<argument name="collectionFactory" xsi:type="object">Smile\ElasticsuiteCatalogGraphQl\Model\ResourceModel\Product\SearchResultCollectionFactory</argument>
</arguments>
</type>

<type name="Magento\CatalogGraphQl\Model\Layer\Context">
<arguments>
<argument name="collectionProvider" xsi:type="object">Smile\ElasticsuiteCatalogGraphQl\Model\Layer\CollectionProvider</argument>
</arguments>
</type>

<type name="Magento\Catalog\Model\Layer\Search">
<arguments>
<argument name="context" xsi:type="object">Magento\CatalogGraphQl\Model\Layer\Context</argument>
</arguments>
</type>

<type name="Magento\Catalog\Model\Layer\Category">
<arguments>
<argument name="context" xsi:type="object">Magento\CatalogGraphQl\Model\Layer\Context</argument>
</arguments>
</type>

<virtualType name="smileEScategoryFilterableAttributesCollection" type="Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory">
<arguments>
<argument name="instanceName" xsi:type="string">Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\FilterableAttribute\Category\Collection</argument>
</arguments>
</virtualType>

<type name="Smile\ElasticsuiteCatalog\Model\Layer\Category\FilterableAttributeList">
<arguments>
<argument name="collectionFactory" xsi:type="object">smileEScategoryFilterableAttributesCollection</argument>
</arguments>
</type>

<virtualType name="categoryFilterList" type="Smile\ElasticsuiteCatalog\Model\Layer\FilterList">
<arguments>
<argument name="filterableAttributes" xsi:type="object">Smile\ElasticsuiteCatalog\Model\Layer\Category\FilterableAttributeList</argument>
<argument name="filters" xsi:type="array">
<item name="attribute" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Attribute</item>
<item name="price" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Price</item>
<item name="decimal" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Decimal</item>
<item name="category" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Category</item>
<item name="boolean" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Boolean</item>
</argument>
</arguments>
</virtualType>

<virtualType name="searchFilterList" type="Smile\ElasticsuiteCatalog\Model\Layer\FilterList">
<arguments>
<argument name="filterableAttributes" xsi:type="object">Smile\ElasticsuiteCatalog\Model\Layer\Search\FilterableAttributeList</argument>
<argument name="filters" xsi:type="array">
<item name="attribute" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Attribute</item>
<item name="price" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Price</item>
<item name="decimal" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Decimal</item>
<item name="category" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Category</item>
<item name="boolean" xsi:type="string">Smile\ElasticsuiteCatalog\Model\Layer\Filter\Boolean</item>
</argument>
</arguments>
</virtualType>

<type name="Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Layer\FiltersProvider">
<arguments>
<argument name="filtersList" xsi:type="array">
<item name="search" xsi:type="object">searchFilterList</item>
<item name="category" xsi:type="object">categoryFilterList</item>
</argument>
</arguments>
</type>

<preference for="Magento\CatalogGraphQl\Model\Resolver\Layer\FiltersProvider" type="Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Layer\FiltersProvider"/>

<type name="Magento\CatalogGraphQl\Model\Resolver\LayerFilters">
<plugin name="search_results_aggregations" type="Smile\ElasticsuiteCatalogGraphQl\Plugin\Resolver\LayerFilters"/>
</type>

<!-- Layered navigation filtering : Aggregation building and modifying -->
<type name="\Smile\ElasticsuiteCatalog\Search\Request\Product\Aggregation\Provider\FilterableAttributes\Modifier\Coverage">
<arguments>
<argument name="coverageRequestBuilder" xsi:type="object">\Smile\ElasticsuiteCatalog\Search\Request\Product\Coverage\Builder</argument>
</arguments>
</type>

<type name="\Smile\ElasticsuiteCatalog\Search\Request\Product\Aggregation\Provider\FilterableAttributes">
<arguments>
<argument name="modifiersPool" xsi:type="array">
<item name="coverageModifier" xsi:type="object">Smile\ElasticsuiteCatalog\Search\Request\Product\Aggregation\Provider\FilterableAttributes\Modifier\Coverage</item>
</argument>
</arguments>
</type>
<<<<<<< HEAD
=======

<virtualType name="elasticsuiteCollectionPreProcessor" type="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CompositeCollectionProcessor">
<arguments>
<argument name="collectionProcessors" xsi:type="array">
<item name="search" xsi:type="object">Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\SearchCriteriaProcessor</item>
</argument>
</arguments>
</virtualType>

<virtualType name="elasticsuiteProductProvider" type="Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch">
<arguments>
<argument name="collectionPreProcessor" xsi:type="object">elasticsuiteCollectionPreProcessor</argument>
</arguments>
</virtualType>

<type name="Smile\ElasticsuiteCatalogGraphQl\Model\Resolver\Products\Query\Search">
<arguments>
<argument name="productProvider" xsi:type="object">elasticsuiteProductProvider</argument>
</arguments>
</type>

<type name="Smile\ElasticsuiteCatalog\Model\Search\Request\Field\Mapper">
<arguments>
<argument name="fieldNameMapping" xsi:type="array">
<item name="category_id" xsi:type="string">category.category_id</item>
</argument>
</arguments>
</type>
>>>>>>> d9e5929... Allow search request field mapping to be configured from di.xml
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
*/
namespace Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalog\Model\Search\Request\Field\Mapper as RequestFieldMapper;
use Smile\ElasticsuiteCore\Api\Index\IndexOperationInterface;
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;
use Smile\ElasticsuiteCore\Api\Index\MappingInterface;
Expand Down Expand Up @@ -70,12 +71,9 @@ class AttributeList
private $mappingHelper;

/**
* @var @array
* @var RequestFieldMapper
*/
private $fieldNameMapping = [
'price' => 'price.price',
'category_ids' => 'category.category_id',
];
private $requestFieldMapper;

/**
* Constructor.
Expand All @@ -84,6 +82,7 @@ class AttributeList
* @param StoreManagerInterface $storeManager Store manager.
* @param IndexOperationInterface $indexManager Search engine index manager.
* @param MappingHelper $mappingHelper Mapping helper.
* @param RequestFieldMapper $requestFieldMapper Search request field mapper.
* @param string $indexName Search engine index name.
* @param string $typeName Search engine type name.
*/
Expand All @@ -92,15 +91,17 @@ public function __construct(
StoreManagerInterface $storeManager,
IndexOperationInterface $indexManager,
MappingHelper $mappingHelper,
RequestFieldMapper $requestFieldMapper,
$indexName = 'catalog_product',
$typeName = 'product'
) {
$this->attributeCollectionFactory = $attributeCollectionFactory;
$this->storeManager = $storeManager;
$this->indexManager = $indexManager;
$this->mappingHelper = $mappingHelper;
$this->requestFieldMapper = $requestFieldMapper;
$this->indexName = $indexName;
$this->typeName = $typeName;
$this->mappingHelper = $mappingHelper;
}

/**
Expand All @@ -112,13 +113,13 @@ public function getAttributeCollection()
{
if ($this->attributeCollection === null) {
$this->attributeCollection = $this->attributeCollectionFactory->create();
$attributeNameMapping = array_flip($this->fieldNameMapping);
$attributeNameMapping = $this->requestFieldMapper->getFieldNameMappings();

$arrayNameCb = function (FieldInterface $field) use ($attributeNameMapping) {
$attributeName = $field->getName();

if (isset($attributeNameMapping[$attributeName])) {
$attributeName = $attributeNameMapping[$attributeName];
if ($fieldMapping = array_search($attributeName, $attributeNameMapping)) {
$attributeName = $fieldMapping;
}

return $attributeName;
Expand All @@ -142,9 +143,7 @@ public function getAttributeCollection()
*/
public function getField($attributeName)
{
if (isset($this->fieldNameMapping[$attributeName])) {
$attributeName = $this->fieldNameMapping[$attributeName];
}
$attributeName = $this->requestFieldMapper->getMappedFieldName($attributeName);

return $this->getMapping()->getField($attributeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,11 @@ public function hasMoreItems()
}

/**
* Retrieve ES filter field.
*
* @return string
* {@inheritDoc}
*/
protected function getFilterField()
{
$field = $this->getAttributeModel()->getAttributeCode();

return $field;
return $this->getAttributeModel()->getAttributeCode();
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/module-elasticsuite-catalog/Model/Layer/Filter/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
namespace Smile\ElasticsuiteCatalog\Model\Layer\Filter;

use Smile\ElasticsuiteCatalog\Model\Search\Request\Field\Mapper as RequestFieldMapper;

/**
* Product category filter implementation.
*
Expand Down Expand Up @@ -54,6 +56,11 @@ class Category extends \Magento\CatalogSearch\Model\Layer\Filter\Category
*/
private $searchContext;

/**
* @var RequestFieldMapper
*/
private $requestFieldMapper;

/**
* Constructor.
*
Expand All @@ -68,6 +75,7 @@ class Category extends \Magento\CatalogSearch\Model\Layer\Filter\Category
* @param \Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $dataProviderFactory Data provider.
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig Scope configuration.
* @param \Smile\ElasticsuiteCore\Api\Search\ContextInterface $context Search Context.
* @param RequestFieldMapper $requestFieldMapper Search request field mapper.
* @param boolean $useUrlRewrites Uses URLs rewrite for rendering.
* @param array $data Custom data.
*/
Expand All @@ -80,6 +88,7 @@ public function __construct(
\Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $dataProviderFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Smile\ElasticsuiteCore\Api\Search\ContextInterface $context,
RequestFieldMapper $requestFieldMapper,
$useUrlRewrites = false,
array $data = []
) {
Expand All @@ -96,6 +105,7 @@ public function __construct(
$this->escaper = $escaper;
$this->dataProvider = $dataProviderFactory->create(['layer' => $this->getLayer()]);
$this->searchContext = $context;
$this->requestFieldMapper = $requestFieldMapper;
$this->useUrlRewrites = ($useUrlRewrites === true) ? (bool) $scopeConfig->isSetFlag(
self::XML_CATEGORY_FILTER_USE_URL_REWRITE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
Expand Down Expand Up @@ -231,7 +241,7 @@ protected function getChildrenCategories()
*/
protected function getFilterField()
{
return 'category.category_id';
return $this->requestFieldMapper->getMappedFieldName('category_ids');
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/module-elasticsuite-catalog/Model/Layer/Filter/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
namespace Smile\ElasticsuiteCatalog\Model\Layer\Filter;

use Smile\ElasticsuiteCatalog\Model\Search\Request\Field\Mapper as RequestFieldMapper;

/**
* Decimal filter model
*
Expand All @@ -36,9 +38,16 @@ class Decimal extends \Magento\CatalogSearch\Model\Layer\Filter\Decimal
*/
private $localeResolver;

/**
* @var RequestFieldMapper
*/
private $requestFieldMapper;

/**
* Decimal constructor.
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory Filter item
* factory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager The Store Manager
Expand All @@ -50,6 +59,8 @@ class Decimal extends \Magento\CatalogSearch\Model\Layer\Filter\Decimal
* @param \Smile\ElasticsuiteCatalog\Model\Layer\Filter\DataProvider\DecimalFactory $dataProviderFactory Decimal DataProvider
* Factory
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver Locale Resolver
* @param RequestFieldMapper $requestFieldMapper Search request field
* mapper
* @param array $data Filter Data
*/
public function __construct(
Expand All @@ -61,6 +72,7 @@ public function __construct(
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Smile\ElasticsuiteCatalog\Model\Layer\Filter\DataProvider\DecimalFactory $dataProviderFactory,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
RequestFieldMapper $requestFieldMapper,
array $data
) {
parent::__construct(
Expand All @@ -72,8 +84,10 @@ public function __construct(
$priceCurrency,
$data
);
$this->localeResolver = $localeResolver;
$this->dataProvider = $dataProviderFactory->create(['layer' => $this->getLayer()]);

$this->dataProvider = $dataProviderFactory->create(['layer' => $this->getLayer()]);
$this->localeResolver = $localeResolver;
$this->requestFieldMapper = $requestFieldMapper;
}

/**
Expand Down Expand Up @@ -102,9 +116,9 @@ protected function _renderRangeLabel($fromValue, $toValue)
*/
private function getFilterField()
{
$field = $this->getAttributeModel()->getAttributeCode();

return $field;
return $this->requestFieldMapper->getMappedFieldName(
$this->getAttributeModel()->getAttributeCode()
);
}

/**
Expand Down
Loading

0 comments on commit acb79b5

Please sign in to comment.