Collection.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\ResourceModel\Fulltext;
  7. use Magento\CatalogSearch\Model\Search\RequestGenerator;
  8. use Magento\Framework\DB\Select;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Framework\Exception\StateException;
  11. use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage;
  12. use Magento\Framework\Search\Response\QueryResponse;
  13. use Magento\Framework\Search\Request\EmptyRequestDataException;
  14. use Magento\Framework\Search\Request\NonExistingRequestNameException;
  15. use Magento\Framework\Api\Search\SearchResultFactory;
  16. use Magento\Framework\Exception\LocalizedException;
  17. use Magento\Framework\App\ObjectManager;
  18. use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
  19. /**
  20. * Fulltext Collection
  21. *
  22. * This collection should be refactored to not have dependencies on MySQL-specific implementation.
  23. *
  24. * @api
  25. * @since 100.0.2
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
  29. {
  30. /**
  31. * @var QueryResponse
  32. * @deprecated 100.1.0
  33. */
  34. protected $queryResponse;
  35. /**
  36. * Catalog search data
  37. *
  38. * @var \Magento\Search\Model\QueryFactory
  39. * @deprecated 100.1.0
  40. */
  41. protected $queryFactory = null;
  42. /**
  43. * @var \Magento\Framework\Search\Request\Builder
  44. * @deprecated 100.1.0
  45. */
  46. private $requestBuilder;
  47. /**
  48. * @var \Magento\Search\Model\SearchEngine
  49. * @deprecated 100.1.0
  50. */
  51. private $searchEngine;
  52. /**
  53. * @var string
  54. */
  55. private $queryText;
  56. /**
  57. * @var string|null
  58. */
  59. private $relevanceOrderDirection = null;
  60. /**
  61. * @var string
  62. */
  63. private $searchRequestName;
  64. /**
  65. * @var \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory
  66. * @deprecated 101.0.0 There must be no dependencies on specific adapter in generic search implementation
  67. */
  68. private $temporaryStorageFactory;
  69. /**
  70. * @var \Magento\Search\Api\SearchInterface
  71. */
  72. private $search;
  73. /**
  74. * @var \Magento\Framework\Api\Search\SearchCriteriaBuilder
  75. */
  76. private $searchCriteriaBuilder;
  77. /**
  78. * @var \Magento\Framework\Api\Search\SearchResultInterface
  79. */
  80. private $searchResult;
  81. /**
  82. * @var SearchResultFactory
  83. */
  84. private $searchResultFactory;
  85. /**
  86. * @var \Magento\Framework\Api\FilterBuilder
  87. */
  88. private $filterBuilder;
  89. /**
  90. * Collection constructor
  91. *
  92. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  93. * @param \Psr\Log\LoggerInterface $logger
  94. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  95. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  96. * @param \Magento\Eav\Model\Config $eavConfig
  97. * @param \Magento\Framework\App\ResourceConnection $resource
  98. * @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
  99. * @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
  100. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  101. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  102. * @param \Magento\Framework\Module\Manager $moduleManager
  103. * @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
  104. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  105. * @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
  106. * @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
  107. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  108. * @param \Magento\Customer\Model\Session $customerSession
  109. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  110. * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
  111. * @param \Magento\Search\Model\QueryFactory $catalogSearchData
  112. * @param \Magento\Framework\Search\Request\Builder $requestBuilder
  113. * @param \Magento\Search\Model\SearchEngine $searchEngine
  114. * @param \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory
  115. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  116. * @param string $searchRequestName
  117. * @param SearchResultFactory|null $searchResultFactory
  118. * @param ProductLimitationFactory|null $productLimitationFactory
  119. * @param MetadataPool|null $metadataPool
  120. *
  121. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  122. */
  123. public function __construct(
  124. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  125. \Psr\Log\LoggerInterface $logger,
  126. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  127. \Magento\Framework\Event\ManagerInterface $eventManager,
  128. \Magento\Eav\Model\Config $eavConfig,
  129. \Magento\Framework\App\ResourceConnection $resource,
  130. \Magento\Eav\Model\EntityFactory $eavEntityFactory,
  131. \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
  132. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  133. \Magento\Store\Model\StoreManagerInterface $storeManager,
  134. \Magento\Framework\Module\Manager $moduleManager,
  135. \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
  136. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  137. \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
  138. \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
  139. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  140. \Magento\Customer\Model\Session $customerSession,
  141. \Magento\Framework\Stdlib\DateTime $dateTime,
  142. \Magento\Customer\Api\GroupManagementInterface $groupManagement,
  143. \Magento\Search\Model\QueryFactory $catalogSearchData,
  144. \Magento\Framework\Search\Request\Builder $requestBuilder,
  145. \Magento\Search\Model\SearchEngine $searchEngine,
  146. \Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory $temporaryStorageFactory,
  147. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  148. $searchRequestName = 'catalog_view_container',
  149. SearchResultFactory $searchResultFactory = null,
  150. ProductLimitationFactory $productLimitationFactory = null,
  151. MetadataPool $metadataPool = null
  152. ) {
  153. $this->queryFactory = $catalogSearchData;
  154. if ($searchResultFactory === null) {
  155. $this->searchResultFactory = \Magento\Framework\App\ObjectManager::getInstance()
  156. ->get(\Magento\Framework\Api\Search\SearchResultFactory::class);
  157. }
  158. parent::__construct(
  159. $entityFactory,
  160. $logger,
  161. $fetchStrategy,
  162. $eventManager,
  163. $eavConfig,
  164. $resource,
  165. $eavEntityFactory,
  166. $resourceHelper,
  167. $universalFactory,
  168. $storeManager,
  169. $moduleManager,
  170. $catalogProductFlatState,
  171. $scopeConfig,
  172. $productOptionFactory,
  173. $catalogUrl,
  174. $localeDate,
  175. $customerSession,
  176. $dateTime,
  177. $groupManagement,
  178. $connection,
  179. $productLimitationFactory,
  180. $metadataPool
  181. );
  182. $this->requestBuilder = $requestBuilder;
  183. $this->searchEngine = $searchEngine;
  184. $this->temporaryStorageFactory = $temporaryStorageFactory;
  185. $this->searchRequestName = $searchRequestName;
  186. }
  187. /**
  188. * Get search.
  189. *
  190. * @deprecated 100.1.0
  191. * @return \Magento\Search\Api\SearchInterface
  192. */
  193. private function getSearch()
  194. {
  195. if ($this->search === null) {
  196. $this->search = ObjectManager::getInstance()->get(\Magento\Search\Api\SearchInterface::class);
  197. }
  198. return $this->search;
  199. }
  200. /**
  201. * Test search.
  202. *
  203. * @deprecated 100.1.0
  204. * @param \Magento\Search\Api\SearchInterface $object
  205. * @return void
  206. * @since 100.1.0
  207. */
  208. public function setSearch(\Magento\Search\Api\SearchInterface $object)
  209. {
  210. $this->search = $object;
  211. }
  212. /**
  213. * Set search criteria builder.
  214. *
  215. * @deprecated 100.1.0
  216. * @return \Magento\Framework\Api\Search\SearchCriteriaBuilder
  217. */
  218. private function getSearchCriteriaBuilder()
  219. {
  220. if ($this->searchCriteriaBuilder === null) {
  221. $this->searchCriteriaBuilder = ObjectManager::getInstance()
  222. ->get(\Magento\Framework\Api\Search\SearchCriteriaBuilder::class);
  223. }
  224. return $this->searchCriteriaBuilder;
  225. }
  226. /**
  227. * Set search criteria builder.
  228. *
  229. * @deprecated 100.1.0
  230. * @param \Magento\Framework\Api\Search\SearchCriteriaBuilder $object
  231. * @return void
  232. * @since 100.1.0
  233. */
  234. public function setSearchCriteriaBuilder(\Magento\Framework\Api\Search\SearchCriteriaBuilder $object)
  235. {
  236. $this->searchCriteriaBuilder = $object;
  237. }
  238. /**
  239. * Get filter builder.
  240. *
  241. * @deprecated 100.1.0
  242. * @return \Magento\Framework\Api\FilterBuilder
  243. */
  244. private function getFilterBuilder()
  245. {
  246. if ($this->filterBuilder === null) {
  247. $this->filterBuilder = ObjectManager::getInstance()->get(\Magento\Framework\Api\FilterBuilder::class);
  248. }
  249. return $this->filterBuilder;
  250. }
  251. /**
  252. * Set filter builder.
  253. *
  254. * @deprecated 100.1.0
  255. * @param \Magento\Framework\Api\FilterBuilder $object
  256. * @return void
  257. * @since 100.1.0
  258. */
  259. public function setFilterBuilder(\Magento\Framework\Api\FilterBuilder $object)
  260. {
  261. $this->filterBuilder = $object;
  262. }
  263. /**
  264. * Apply attribute filter to facet collection
  265. *
  266. * @param string $field
  267. * @param mixed|null $condition
  268. * @return $this
  269. */
  270. public function addFieldToFilter($field, $condition = null)
  271. {
  272. if ($this->searchResult !== null) {
  273. throw new \RuntimeException('Illegal state');
  274. }
  275. $this->getSearchCriteriaBuilder();
  276. $this->getFilterBuilder();
  277. if (!is_array($condition) || !in_array(key($condition), ['from', 'to'], true)) {
  278. $this->filterBuilder->setField($field);
  279. $this->filterBuilder->setValue($condition);
  280. $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
  281. } else {
  282. if (!empty($condition['from'])) {
  283. $this->filterBuilder->setField("{$field}.from");
  284. $this->filterBuilder->setValue($condition['from']);
  285. $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
  286. }
  287. if (!empty($condition['to'])) {
  288. $this->filterBuilder->setField("{$field}.to");
  289. $this->filterBuilder->setValue($condition['to']);
  290. $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
  291. }
  292. }
  293. return $this;
  294. }
  295. /**
  296. * Add search query filter
  297. *
  298. * @param string $query
  299. * @return $this
  300. */
  301. public function addSearchFilter($query)
  302. {
  303. $this->queryText = trim($this->queryText . ' ' . $query);
  304. return $this;
  305. }
  306. /**
  307. * @inheritdoc
  308. */
  309. protected function _renderFiltersBefore()
  310. {
  311. $this->getSearchCriteriaBuilder();
  312. $this->getFilterBuilder();
  313. $this->getSearch();
  314. if ($this->queryText) {
  315. $this->filterBuilder->setField('search_term');
  316. $this->filterBuilder->setValue($this->queryText);
  317. $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
  318. }
  319. $priceRangeCalculation = $this->_scopeConfig->getValue(
  320. \Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION,
  321. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  322. );
  323. if ($priceRangeCalculation) {
  324. $this->filterBuilder->setField('price_dynamic_algorithm');
  325. $this->filterBuilder->setValue($priceRangeCalculation);
  326. $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
  327. }
  328. $searchCriteria = $this->searchCriteriaBuilder->create();
  329. $searchCriteria->setRequestName($this->searchRequestName);
  330. try {
  331. $this->searchResult = $this->getSearch()->search($searchCriteria);
  332. } catch (EmptyRequestDataException $e) {
  333. /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
  334. $this->searchResult = $this->searchResultFactory->create()->setItems([]);
  335. } catch (NonExistingRequestNameException $e) {
  336. $this->_logger->error($e->getMessage());
  337. throw new LocalizedException(__('An error occurred. For details, see the error log.'));
  338. }
  339. $temporaryStorage = $this->temporaryStorageFactory->create();
  340. $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
  341. $this->getSelect()->joinInner(
  342. [
  343. 'search_result' => $table->getName(),
  344. ],
  345. 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID,
  346. []
  347. );
  348. if ($this->relevanceOrderDirection) {
  349. $this->getSelect()->order(
  350. 'search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->relevanceOrderDirection
  351. );
  352. }
  353. return parent::_renderFiltersBefore();
  354. }
  355. /**
  356. * @inheritdoc
  357. * @since 100.2.3
  358. */
  359. protected function _beforeLoad()
  360. {
  361. /*
  362. * This order is required to force search results be the same
  363. * for the same requests and products with the same relevance
  364. * NOTE: this does not replace existing orders but ADDs one more
  365. */
  366. $this->setOrder('entity_id');
  367. return parent::_beforeLoad();
  368. }
  369. /**
  370. * Render filters.
  371. *
  372. * @return $this
  373. */
  374. protected function _renderFilters()
  375. {
  376. $this->_filters = [];
  377. return parent::_renderFilters();
  378. }
  379. /**
  380. * Set Order field
  381. *
  382. * @param string $attribute
  383. * @param string $dir
  384. * @return $this
  385. */
  386. public function setOrder($attribute, $dir = Select::SQL_DESC)
  387. {
  388. if ($attribute === 'relevance') {
  389. $this->relevanceOrderDirection = $dir;
  390. } else {
  391. parent::setOrder($attribute, $dir);
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Stub method for compatibility with other search engines
  397. *
  398. * @return $this
  399. */
  400. public function setGeneralDefaultQuery()
  401. {
  402. return $this;
  403. }
  404. /**
  405. * Return field faceted data from faceted search result
  406. *
  407. * @param string $field
  408. * @return array
  409. * @throws StateException
  410. */
  411. public function getFacetedData($field)
  412. {
  413. $this->_renderFilters();
  414. $result = [];
  415. $aggregations = $this->searchResult->getAggregations();
  416. // This behavior is for case with empty object when we got EmptyRequestDataException
  417. if (null !== $aggregations) {
  418. $bucket = $aggregations->getBucket($field . RequestGenerator::BUCKET_SUFFIX);
  419. if ($bucket) {
  420. foreach ($bucket->getValues() as $value) {
  421. $metrics = $value->getMetrics();
  422. $result[$metrics['value']] = $metrics;
  423. }
  424. } else {
  425. throw new StateException(__("The bucket doesn't exist."));
  426. }
  427. }
  428. return $result;
  429. }
  430. /**
  431. * Specify category filter for product collection
  432. *
  433. * @param \Magento\Catalog\Model\Category $category
  434. * @return $this
  435. */
  436. public function addCategoryFilter(\Magento\Catalog\Model\Category $category)
  437. {
  438. $this->addFieldToFilter('category_ids', $category->getId());
  439. return parent::addCategoryFilter($category);
  440. }
  441. /**
  442. * Set product visibility filter for enabled products
  443. *
  444. * @param array $visibility
  445. * @return $this
  446. */
  447. public function setVisibility($visibility)
  448. {
  449. $this->addFieldToFilter('visibility', $visibility);
  450. return parent::setVisibility($visibility);
  451. }
  452. }