6
6
7
7
namespace Magento \CatalogSearch \Model \ResourceModel \Fulltext \Collection ;
8
8
9
- use Magento \Framework \Api \Search \SearchResultInterface ;
10
9
use 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 ;
11
13
12
14
/**
13
15
* Resolve specific attributes for search criteria.
@@ -27,6 +29,11 @@ class SearchResultApplier implements SearchResultApplierInterface
27
29
*/
28
30
private $ searchResult ;
29
31
32
+ /**
33
+ * @var TemporaryStorageFactory
34
+ */
35
+ private $ temporaryStorageFactory ;
36
+
30
37
/**
31
38
* @var array
32
39
*/
@@ -35,15 +42,18 @@ class SearchResultApplier implements SearchResultApplierInterface
35
42
/**
36
43
* @param Collection $collection
37
44
* @param SearchResultInterface $searchResult
45
+ * @param TemporaryStorageFactory $temporaryStorageFactory
38
46
* @param array $orders
39
47
*/
40
48
public function __construct (
41
49
Collection $ collection ,
42
50
SearchResultInterface $ searchResult ,
51
+ TemporaryStorageFactory $ temporaryStorageFactory ,
43
52
array $ orders
44
53
) {
45
54
$ this ->collection = $ collection ;
46
55
$ this ->searchResult = $ searchResult ;
56
+ $ this ->temporaryStorageFactory = $ temporaryStorageFactory ;
47
57
$ this ->orders = $ orders ;
48
58
}
49
59
@@ -52,22 +62,21 @@ public function __construct(
52
62
*/
53
63
public function apply ()
54
64
{
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 ());
63
67
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
+ );
66
75
67
76
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
+ );
71
80
}
72
81
}
73
82
}
0 commit comments