Skip to content

Commit

Permalink
Merge pull request Smile-SA#15 from Elastic-Suite/feature-explain
Browse files Browse the repository at this point in the history
Feature explain
  • Loading branch information
rbayet authored Mar 4, 2021
2 parents 98b191b + cee431d commit 72e24fd
Show file tree
Hide file tree
Showing 36 changed files with 3,224 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"smile/module-elasticsuite-behavioral-data": "self.version",
"smile/module-elasticsuite-behavioral-optimizer": "self.version",
"smile/module-elasticsuite-catalog-optimizer-customer-segment": "self.version",
"smile/module-elasticsuite-explain": "self.version",
"smile/module-elasticsuite-facet-recommender": "self.version",
"smile/module-elasticsuite-instant-search": "self.version",
"smile/module-elasticsuite-recommender": "self.version",
Expand All @@ -62,6 +63,7 @@
"src/module-elasticsuite-behavioral-data/registration.php",
"src/module-elasticsuite-behavioral-optimizer/registration.php",
"src/module-elasticsuite-catalog-optimizer-customer-segment/registration.php",
"src/module-elasticsuite-explain/registration.php",
"src/module-elasticsuite-facet-recommender/registration.php",
"src/module-elasticsuite-instant-search/registration.php",
"src/module-elasticsuite-recommender/registration.php",
Expand All @@ -74,6 +76,7 @@
"Smile\\ElasticsuiteBehavioralData\\": "src/module-elasticsuite-behavioral-data",
"Smile\\ElasticsuiteBehavioralOptimizer\\": "src/module-elasticsuite-behavioral-optimizer",
"Smile\\ElasticsuiteCatalogOptimizerCustomerSegment\\": "src/module-elasticsuite-catalog-optimizer-customer-segment",
"Smile\\ElasticsuiteExplain\\": "src/module-elasticsuite-explain",
"Smile\\ElasticsuiteFacetRecommender\\": "src/module-elasticsuite-facet-recommender",
"Smile\\ElasticsuiteInstantSearch\\": "src/module-elasticsuite-instant-search",
"Smile\\ElasticsuiteRecommender\\": "src/module-elasticsuite-recommender",
Expand Down
16 changes: 16 additions & 0 deletions src/module-elasticsuite-explain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Standard composer ignored paths
composer.phar
/vendor/

# Standard IDEs ignored paths
.metadata
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.project
.buildpath

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\ElasticsuiteExplain
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteExplain\Controller\Adminhtml\Explain;

use Magento\Backend\App\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;

/**
* Explain Adminhtml Index controller.
*
* @category Smile
* @package Smile\ElasticsuiteExplain
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class Index extends Action
{
/**
* {@inheritDoc}
*/
public function execute(): ResultInterface
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

$resultPage->setActiveMenu('Smile_ElasticsuiteExplain::explain')
->addBreadcrumb(__('Explain'), __('Explain'));

$resultPage->getConfig()->getTitle()->prepend(__('Explain search and navigation results'));
$resultPage->addBreadcrumb(__('Explain'), __('Explain'));

return $resultPage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
<?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\ElasticsuiteExplain
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteExplain\Controller\Adminhtml\Explain;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Search\Model\QueryFactory;
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterfaceFactory;
use Smile\ElasticsuiteExplain\Model\ResultFactory as ResultModelFactory;
use Smile\ElasticsuiteCore\Api\Search\ContextInterface;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteCore\Search\Request\ContainerConfigurationFactory;

/**
* Explain Adminhtml Index controller.
*
* @category Smile
* @package Smile\ElasticsuiteExplain
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class Results extends Action
{
/**
* @var \Smile\ElasticsuiteExplain\Model\ResultFactory
*/
private $resultModelFactory;

/**
* @var \Magento\Framework\Json\Helper\Data
*/
private $jsonHelper;

/**
* @var OptimizerInterfaceFactory
*/
private $optimizerFactory;

/**
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var \Smile\ElasticsuiteCore\Search\Request\ContainerConfigurationFactory
*/
private $containerConfigFactory;

/**
* @var \Magento\Search\Model\QueryFactory
*/
private $queryFactory;

/**
* @var \Smile\ElasticsuiteCore\Api\Search\ContextInterface
*/
private $searchContext;

/**
* Constructor.
*
* @param Context $context Controller context.
* @param ResultModelFactory $resultModelFactory Result model factory.
* @param CategoryRepositoryInterface $categoryRepository Category Repository
* @param OptimizerInterfaceFactory $optimizerFactory OptimzerFactory
* @param ContainerConfigurationFactory $containerConfigFactory Container Configuration Factory
* @param JsonHelper $jsonHelper JSON Helper.
* @param QueryFactory $queryFactory Query Factory.
* @param ContextInterface $searchContext Search context.
*/
public function __construct(
Context $context,
ResultModelFactory $resultModelFactory,
CategoryRepositoryInterface $categoryRepository,
OptimizerInterfaceFactory $optimizerFactory,
ContainerConfigurationFactory $containerConfigFactory,
JsonHelper $jsonHelper,
QueryFactory $queryFactory,
ContextInterface $searchContext
) {
parent::__construct($context);

$this->optimizerFactory = $optimizerFactory;
$this->categoryRepository = $categoryRepository;
$this->resultModelFactory = $resultModelFactory;
$this->containerConfigFactory = $containerConfigFactory;
$this->jsonHelper = $jsonHelper;
$this->queryFactory = $queryFactory;
$this->searchContext = $searchContext;
}

/**
* {@inheritDoc}
*/
public function execute()
{
$responseData = $this->getResultObject()->getData();
$json = $this->jsonHelper->jsonEncode($responseData);

$this->getResponse()->representJson($json);
}

/**
* Check if allowed to manage optimizer.
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Smile_ElasticsuiteCatalogOptimizer::manage');
}

/**
* Load and initialize the result model.
*
* @return \Smile\ElasticsuiteExplain\Model\Result
*/
private function getResultObject()
{
$pageSize = $this->getPageSize();
$queryText = $this->getQueryText();
$category = $this->getCategory();
$containerConfig = $this->getContainerConfiguration();

$this->updateSearchContext($this->getStoreId(), $category, $queryText);

return $this->resultModelFactory->create(
[
'containerConfig' => $containerConfig,
'category' => $category,
'queryText' => $queryText,
'size' => $pageSize,
]
);
}

/**
* Load current category using the request params.
*
* @return CategoryInterface
*/
private function getCategory()
{
$storeId = $this->getStoreId();
$categoryId = $this->getCategoryId();
$category = null;

if ($this->getCategoryId()) {
$category = $this->categoryRepository->get($categoryId, $storeId);
}

return $category;
}

/**
* Return the preview page size.
*
* @return int
*/
private function getPageSize()
{
return (int) $this->getRequest()->getParam('page_size');
}

/**
* Return the container to preview.
*
* @return ContainerConfigurationInterface
*/
private function getContainerConfiguration()
{
$containerName = $this->getRequest()->getParam('search_container_preview');

$containerConfig = $this->containerConfigFactory->create(
['containerName' => $containerName, 'storeId' => $this->getStoreId()]
);

return $containerConfig;
}

/**
* Return the query text to preview.
*
* @return string
*/
private function getQueryText()
{
$queryText = trim(strtolower((string) $this->getRequest()->getParam('query_text_preview', '')));

if ($queryText == '') {
$queryText = null;
}

return $queryText;
}

/**
* Return the category to preview.
*
* @return int
*/
private function getCategoryId()
{
return $this->getRequest()->getParam('category_preview');
}

/**
* Return the store id to preview.
*
* @return int
*/
private function getStoreId()
{
return $this->getRequest()->getParam('store_id');
}

/**
* Update the search context using current store id, category or query text.
*
* @param integer $storeId Store id.
* @param CategoryInterface $category Category.
* @param string $queryText Fulltext query text.
*
* @return void
*/
private function updateSearchContext($storeId, $category, $queryText)
{
$this->searchContext->setStoreId($storeId);

if ((string) $queryText !== '') {
try {
$query = $this->queryFactory->create()->loadByQueryText($queryText);
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
$query = $this->queryFactory->create();
}

if ((string) $query->getQueryText() === '') {
$query->setQueryText($queryText);
}

$this->searchContext->setCurrentSearchQuery($query);
} elseif ($category && $category->getId()) {
$this->searchContext->setCurrentCategory($category);
}
}
}
Loading

0 comments on commit 72e24fd

Please sign in to comment.