Skip to content

Commit 11452b8

Browse files
author
Andrii Kasian
committed
Merge remote-tracking branch 'origin/MAGETWO-39542' into S70pr
# Conflicts: # lib/internal/Magento/Framework/Search/Response/Aggregation/Value.php # lib/internal/Magento/Framework/Search/Response/Bucket.php
2 parents 5d93c4c + 5bc9396 commit 11452b8

31 files changed

+1117
-40
lines changed

app/code/Magento/Catalog/Api/ProductRepositoryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@ public function deleteById($sku);
7272
* @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
7373
*/
7474
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
75+
76+
/**
77+
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria
78+
* @return \Magento\Framework\Api\Search\SearchResultInterface
79+
*/
80+
public function search(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria);
7581
}

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
7676
protected $linkInitializer;
7777

7878
/*
79-
* @param \Magento\Catalog\Model\Product\LinkTypeProvider
79+
* @var \Magento\Catalog\Model\Product\LinkTypeProvider
8080
*/
8181
protected $linkTypeProvider;
8282

8383
/*
84-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
84+
* @var \Magento\Store\Model\StoreManagerInterface
8585
*/
8686
protected $storeManager;
8787

@@ -140,6 +140,25 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
140140
*/
141141
protected $extensionAttributesJoinProcessor;
142142

143+
/**
144+
* @var \Magento\Framework\Search\Request\Builder
145+
*/
146+
private $requestBuilder;
147+
148+
/**
149+
* @var \Magento\Framework\Search\SearchEngineInterface
150+
*/
151+
private $searchEngine;
152+
153+
/**
154+
* @var SearchResponseBuilder
155+
*/
156+
private $searchResponseBuilder;
157+
/**
158+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
159+
*/
160+
private $scopeConfig;
161+
143162
/**
144163
* @param ProductFactory $productFactory
145164
* @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $initializationHelper
@@ -162,6 +181,10 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
162181
* @param \Magento\Eav\Model\Config $eavConfig
163182
* @param ImageProcessorInterface $imageProcessor
164183
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
184+
* @param \Magento\Framework\Search\Request\Builder $requestBuilder
185+
* @param \Magento\Framework\Search\SearchEngineInterface $searchEngine
186+
* @param SearchResponseBuilder $searchResponseBuilder
187+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
165188
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
166189
*/
167190
public function __construct(
@@ -185,7 +208,11 @@ public function __construct(
185208
MimeTypeExtensionMap $mimeTypeExtensionMap,
186209
\Magento\Eav\Model\Config $eavConfig,
187210
ImageProcessorInterface $imageProcessor,
188-
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
211+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
212+
\Magento\Framework\Search\Request\Builder $requestBuilder,
213+
\Magento\Framework\Search\SearchEngineInterface $searchEngine,
214+
SearchResponseBuilder $searchResponseBuilder,
215+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
189216
) {
190217
$this->productFactory = $productFactory;
191218
$this->collectionFactory = $collectionFactory;
@@ -208,6 +235,10 @@ public function __construct(
208235
$this->eavConfig = $eavConfig;
209236
$this->imageProcessor = $imageProcessor;
210237
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
238+
$this->requestBuilder = $requestBuilder;
239+
$this->searchEngine = $searchEngine;
240+
$this->searchResponseBuilder = $searchResponseBuilder;
241+
$this->scopeConfig = $scopeConfig;
211242
}
212243

213244
/**
@@ -690,6 +721,45 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
690721
return $searchResult;
691722
}
692723

724+
/**
725+
* @param \Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria
726+
* @return \Magento\Framework\Api\Search\SearchResultInterface
727+
*/
728+
public function search(\Magento\Framework\Api\Search\SearchCriteriaInterface $searchCriteria)
729+
{
730+
$this->requestBuilder->setRequestName($searchCriteria->getRequestName());
731+
732+
$searchTerm = $searchCriteria->getSearchTerm();
733+
if (!empty($searchTerm)) {
734+
$this->requestBuilder->bind('search_term', $searchTerm);
735+
}
736+
737+
$storeId = $this->storeManager->getStore(true)->getId();
738+
$this->requestBuilder->bindDimension('scope', $storeId);
739+
740+
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
741+
foreach ($filterGroup->getFilters() as $filter) {
742+
$this->addFieldToFilter($filter->getField(), $filter->getValue());
743+
}
744+
}
745+
746+
$priceRangeCalculation = $this->scopeConfig->getValue(
747+
\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION,
748+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
749+
);
750+
if ($priceRangeCalculation) {
751+
$this->requestBuilder->bind('price_dynamic_algorithm', $priceRangeCalculation);
752+
}
753+
754+
$this->requestBuilder->setFrom($searchCriteria->getCurrentPage() * $searchCriteria->getPageSize());
755+
$this->requestBuilder->setSize($searchCriteria->getPageSize());
756+
$request = $this->requestBuilder->create();
757+
$searchResponse = $this->searchEngine->search($request);
758+
759+
return $this->searchResponseBuilder->build($searchResponse)
760+
->setSearchCriteria($searchCriteria);
761+
}
762+
693763
/**
694764
* Helper function that adds a FilterGroup to the collection.
695765
*
@@ -710,4 +780,26 @@ protected function addFilterGroupToCollection(
710780
$collection->addFieldToFilter($fields);
711781
}
712782
}
783+
784+
/**
785+
* Apply attribute filter to facet collection
786+
*
787+
* @param string $field
788+
* @param null $condition
789+
* @return $this
790+
*/
791+
private function addFieldToFilter($field, $condition = null)
792+
{
793+
if (!is_array($condition) || !in_array(key($condition), ['from', 'to'])) {
794+
$this->requestBuilder->bind($field, $condition);
795+
} else {
796+
if (!empty($condition['from'])) {
797+
$this->requestBuilder->bind("{$field}.from", $condition['from']);
798+
}
799+
if (!empty($condition['to'])) {
800+
$this->requestBuilder->bind("{$field}.to", $condition['to']);
801+
}
802+
}
803+
return $this;
804+
}
713805
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model;
7+
8+
use Magento\Framework\Api\Search\SearchResultInterface;
9+
use Magento\Framework\Api\Search\DocumentFactory;
10+
use Magento\Framework\Api\Search\SearchResultFactory;
11+
use Magento\Framework\Search\ResponseInterface;
12+
13+
class SearchResponseBuilder
14+
{
15+
/**
16+
* @var DocumentFactory
17+
*/
18+
private $documentFactory;
19+
20+
/**
21+
* @var SearchResultFactory
22+
*/
23+
private $searchResultFactory;
24+
25+
/**
26+
* @param SearchResultFactory $searchResultFactory
27+
* @param DocumentFactory $documentFactory
28+
*/
29+
public function __construct(
30+
SearchResultFactory $searchResultFactory,
31+
DocumentFactory $documentFactory
32+
) {
33+
$this->documentFactory = $documentFactory;
34+
$this->searchResultFactory = $searchResultFactory;
35+
}
36+
37+
/**
38+
* @param ResponseInterface $response
39+
* @return SearchResultInterface
40+
*/
41+
public function build(ResponseInterface $response)
42+
{
43+
/** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
44+
$searchResult = $this->searchResultFactory->create();
45+
46+
/** @var \Magento\Framework\Api\Search\DocumentInterface[] $documents */
47+
$documents = [];
48+
49+
/** @var \Magento\Framework\Search\Document $responseDocument */
50+
foreach ($response as $responseDocument) {
51+
$document = $this->documentFactory->create();
52+
53+
/** @var \Magento\Framework\Search\DocumentField $field */
54+
foreach ($responseDocument as $field) {
55+
$document->setCustomAttribute($field->getName(), $field->getValue());
56+
}
57+
58+
$document->setId($responseDocument->getId());
59+
60+
$documents[] = $document;
61+
}
62+
$searchResult->setItems($documents);
63+
$searchResult->setAggregations($response->getAggregations());
64+
$searchResult->setTotalCount(count($documents));
65+
66+
return $searchResult;
67+
}
68+
}

0 commit comments

Comments
 (0)