forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Smile-SA#14 - First implementation of the search API with ES.
- Loading branch information
Aurélien FOUCRET
committed
Mar 21, 2017
1 parent
09519ab
commit 896ed7f
Showing
3 changed files
with
166 additions
and
7 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,149 @@ | ||
<?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\ElasticsuiteCore | ||
* @author Romain Ruaud <[email protected]> | ||
* @copyright 2016 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticSuiteCore\Model; | ||
|
||
/** | ||
* SearchInterface implementation using elasticsuite. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Romain Ruaud <[email protected]> | ||
*/ | ||
class Search implements \Magento\Framework\Api\Search\SearchInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Builder | ||
*/ | ||
private $requestBuilder; | ||
|
||
/** | ||
* @var \Magento\Framework\App\ScopeResolverInterface | ||
*/ | ||
private $scopeResolver; | ||
|
||
/** | ||
* @var \Magento\Search\Model\SearchEngine | ||
*/ | ||
private $searchEngine; | ||
|
||
/** | ||
* @var \Magento\Framework\Search\SearchResponseBuilder | ||
*/ | ||
private $searchResponseBuilder; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver | ||
* @param \Magento\Search\Model\SearchEngine $searchEngine | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Builder $requestBuilder | ||
* @param \Magento\Framework\Search\SearchResponseBuilder $searchResponseBuilder | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\App\ScopeResolverInterface $scopeResolver, | ||
\Magento\Search\Model\SearchEngine $searchEngine, | ||
\Smile\ElasticsuiteCore\Search\Request\Builder $requestBuilder, | ||
\Magento\Framework\Search\SearchResponseBuilder $searchResponseBuilder | ||
) { | ||
$this->requestBuilder = $requestBuilder; | ||
$this->scopeResolver = $scopeResolver; | ||
$this->searchEngine = $searchEngine; | ||
$this->searchResponseBuilder = $searchResponseBuilder; | ||
} | ||
|
||
|
||
/** | ||
* Execute search. | ||
* | ||
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria Search criteria. | ||
* | ||
* @return \Magento\Framework\Api\Search\SearchResultInterface | ||
*/ | ||
public function search(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria) | ||
{ | ||
$searchRequest = $this->getSearchRequest($searchCriteria); | ||
$searchResponse = $this->searchEngine->search($searchRequest); | ||
$searchResult = $this->searchResponseBuilder->build($searchResponse); | ||
|
||
$totalCount = $searchResponse->count(); | ||
$searchResult->setTotalCount($totalCount); | ||
$searchResult->setSearchCriteria($searchCriteria); | ||
|
||
return $searchResult; | ||
} | ||
|
||
/** | ||
* | ||
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria | ||
* | ||
* @return \Smile\ElasticsuiteCore\Search\RequestInterface | ||
*/ | ||
private function getSearchRequest(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria) | ||
{ | ||
$request = $this->requestBuilder->create( | ||
$this->scopeResolver->getScope()->getId(), | ||
$searchCriteria->getRequestName(), | ||
max(0, $searchCriteria->getPageSize() * ($searchCriteria->getCurrentPage() - 1)), | ||
$searchCriteria->getPageSize(), | ||
$this->getFulltextQuery($searchCriteria), | ||
$this->getSortOrders($searchCriteria), | ||
$this->getFilters($searchCriteria) | ||
); | ||
|
||
return $request; | ||
} | ||
|
||
/** | ||
* | ||
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria | ||
* | ||
* @return NULL|string | ||
*/ | ||
private function getFulltextQuery(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria) | ||
{ | ||
$fulltextQuery = null; | ||
|
||
foreach ($searchCriteria->getFilterGroups() as $filterGroup) { | ||
foreach ($filterGroup->getFilters() as $filter) { | ||
if ($filter->getField() === 'search_term') { | ||
$fulltextQuery = $filter->getValue(); | ||
} | ||
} | ||
} | ||
|
||
return $fulltextQuery; | ||
} | ||
|
||
/** | ||
* | ||
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria | ||
*/ | ||
private function getSortOrders(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria) | ||
{ | ||
$sort = []; | ||
//@todo | ||
return $sort; | ||
} | ||
|
||
/** | ||
* | ||
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria | ||
*/ | ||
private function getFilters(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria) | ||
{ | ||
$filters = []; | ||
//@todo | ||
return $filters; | ||
} | ||
} |
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