6
6
use AlgoliaSearch \AlgoliaConnectionException ;
7
7
use Magento \Framework \App \ResourceConnection ;
8
8
use Magento \Framework \DB \Ddl \Table ;
9
+ use Magento \Framework \DB \Select ;
9
10
use Magento \Framework \Search \Adapter \Mysql \Aggregation \Builder as AggregationBuilder ;
10
11
use Magento \Framework \Search \Adapter \Mysql \DocumentFactory ;
11
12
use Magento \Framework \Search \Adapter \Mysql \Mapper ;
@@ -40,6 +41,11 @@ class Algolia implements AdapterInterface
40
41
/** @var DocumentFactory */
41
42
private $ documentFactory ;
42
43
44
+ private $ countSqlSkipParts = [
45
+ \Magento \Framework \DB \Select::LIMIT_COUNT => true ,
46
+ \Magento \Framework \DB \Select::LIMIT_OFFSET => true ,
47
+ ];
48
+
43
49
/**
44
50
* @param Mapper $mapper
45
51
* @param ResponseFactory $responseFactory
@@ -103,6 +109,7 @@ public function query(RequestInterface $request)
103
109
$ response = [
104
110
'documents ' => $ documents ,
105
111
'aggregations ' => $ aggregations ,
112
+ 'total ' => 0 ,
106
113
];
107
114
108
115
return $ this ->responseFactory ->create ($ response );
@@ -120,6 +127,7 @@ private function nativeQuery(RequestInterface $request)
120
127
$ response = [
121
128
'documents ' => $ documents ,
122
129
'aggregations ' => $ aggregations ,
130
+ 'total ' => $ this ->getSize ($ query ),
123
131
];
124
132
125
133
return $ this ->responseFactory ->create ($ response );
@@ -130,6 +138,29 @@ private function getApiDocument($document)
130
138
return $ this ->documentFactory ->create ($ document );
131
139
}
132
140
141
+ private function getSize (Select $ query )
142
+ {
143
+ $ sql = $ this ->getSelectCountSql ($ query );
144
+ $ parentSelect = $ this ->getConnection ()->select ();
145
+ $ parentSelect ->from (['core_select ' => $ sql ]);
146
+ $ parentSelect ->reset (\Magento \Framework \DB \Select::COLUMNS );
147
+ $ parentSelect ->columns ('COUNT(*) ' );
148
+ $ totalRecords = $ this ->getConnection ()->fetchOne ($ parentSelect );
149
+
150
+ return intval ($ totalRecords );
151
+ }
152
+
153
+ private function getSelectCountSql (Select $ query )
154
+ {
155
+ foreach ($ this ->countSqlSkipParts as $ part => $ toSkip ) {
156
+ if ($ toSkip ) {
157
+ $ query ->reset ($ part );
158
+ }
159
+ }
160
+
161
+ return $ query ;
162
+ }
163
+
133
164
/**
134
165
* Executes query and return raw response
135
166
*
0 commit comments