66
77namespace Magento \CatalogSearch \Model \ResourceModel \Fulltext \Collection ;
88
9- use Magento \Framework \Api \Search \SearchResultInterface ;
109use Magento \Framework \Data \Collection ;
10+ use Magento \Framework \Search \Adapter \Mysql \TemporaryStorage ;
11+ use Magento \Framework \Search \Adapter \Mysql \TemporaryStorageFactory ;
12+ use Magento \Framework \Api \Search \SearchResultInterface ;
1113
1214/**
1315 * Resolve specific attributes for search criteria.
@@ -27,6 +29,11 @@ class SearchResultApplier implements SearchResultApplierInterface
2729 */
2830 private $ searchResult ;
2931
32+ /**
33+ * @var TemporaryStorageFactory
34+ */
35+ private $ temporaryStorageFactory ;
36+
3037 /**
3138 * @var array
3239 */
@@ -35,15 +42,18 @@ class SearchResultApplier implements SearchResultApplierInterface
3542 /**
3643 * @param Collection $collection
3744 * @param SearchResultInterface $searchResult
45+ * @param TemporaryStorageFactory $temporaryStorageFactory
3846 * @param array $orders
3947 */
4048 public function __construct (
4149 Collection $ collection ,
4250 SearchResultInterface $ searchResult ,
51+ TemporaryStorageFactory $ temporaryStorageFactory ,
4352 array $ orders
4453 ) {
4554 $ this ->collection = $ collection ;
4655 $ this ->searchResult = $ searchResult ;
56+ $ this ->temporaryStorageFactory = $ temporaryStorageFactory ;
4757 $ this ->orders = $ orders ;
4858 }
4959
@@ -52,22 +62,21 @@ public function __construct(
5262 */
5363 public function apply ()
5464 {
55- if (empty ($ this ->searchResult ->getItems ())) {
56- $ this ->collection ->getSelect ()->where ('NULL ' );
57- return ;
58- }
59- $ ids = [];
60- foreach ($ this ->searchResult ->getItems () as $ item ) {
61- $ ids [] = (int )$ item ->getId ();
62- }
65+ $ temporaryStorage = $ this ->temporaryStorageFactory ->create ();
66+ $ table = $ temporaryStorage ->storeApiDocuments ($ this ->searchResult ->getItems ());
6367
64- $ orderList = implode (', ' , $ ids );
65- $ this ->collection ->getSelect ()->where ('e.entity_id IN (?) ' , $ ids );
68+ $ this ->collection ->getSelect ()->joinInner (
69+ [
70+ 'search_result ' => $ table ->getName (),
71+ ],
72+ 'e.entity_id = search_result. ' . TemporaryStorage::FIELD_ENTITY_ID ,
73+ []
74+ );
6675
6776 if (isset ($ this ->orders ['relevance ' ])) {
68- $ this ->collection ->getSelect ()
69- -> reset (\ Magento \ Framework \ DB \Select:: ORDER )
70- -> order ( new \ Magento \ Framework \ DB \ Sql \ Expression ( " FIELD(e.entity_id, $ orderList ) " ) );
77+ $ this ->collection ->getSelect ()-> order (
78+ ' search_result. ' . TemporaryStorage:: FIELD_SCORE . ' ' . $ this -> orders [ ' relevance ' ]
79+ );
7180 }
7281 }
7382}
0 commit comments