Skip to content

Commit 5e744fb

Browse files
authored
Merge pull request #815 from algolia/develop
Develop
2 parents 67d5b38 + 5d88ad8 commit 5e744fb

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed

Adapter/Algolia.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AlgoliaSearch\AlgoliaConnectionException;
77
use Magento\Framework\App\ResourceConnection;
88
use Magento\Framework\DB\Ddl\Table;
9+
use Magento\Framework\DB\Select;
910
use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder;
1011
use Magento\Framework\Search\Adapter\Mysql\DocumentFactory;
1112
use Magento\Framework\Search\Adapter\Mysql\Mapper;
@@ -40,6 +41,11 @@ class Algolia implements AdapterInterface
4041
/** @var DocumentFactory */
4142
private $documentFactory;
4243

44+
private $countSqlSkipParts = [
45+
Select::LIMIT_COUNT => true,
46+
Select::LIMIT_OFFSET => true,
47+
];
48+
4349
/**
4450
* @param Mapper $mapper
4551
* @param ResponseFactory $responseFactory
@@ -85,12 +91,13 @@ public function query(RequestInterface $request)
8591

8692
$temporaryStorage = $this->temporaryStorageFactory->create();
8793
$documents = [];
94+
$totalHits = 0;
8895
$table = null;
8996

9097
try {
9198
// If instant search is on, do not make a search query unless SEO request is set to 'Yes'
9299
if (!$this->adapterHelper->isInstantEnabled() || $this->adapterHelper->makeSeoRequest()) {
93-
$documents = $this->adapterHelper->getDocumentsFromAlgolia();
100+
list($documents, $totalHits) = $this->adapterHelper->getDocumentsFromAlgolia();
94101
}
95102

96103
$apiDocuments = array_map([$this, 'getApiDocument'], $documents);
@@ -103,6 +110,7 @@ public function query(RequestInterface $request)
103110
$response = [
104111
'documents' => $documents,
105112
'aggregations' => $aggregations,
113+
'total' => $totalHits,
106114
];
107115

108116
return $this->responseFactory->create($response);
@@ -120,6 +128,7 @@ private function nativeQuery(RequestInterface $request)
120128
$response = [
121129
'documents' => $documents,
122130
'aggregations' => $aggregations,
131+
'total' => $this->getSize($query),
123132
];
124133

125134
return $this->responseFactory->create($response);
@@ -153,4 +162,39 @@ private function getConnection()
153162
{
154163
return $this->resource->getConnection();
155164
}
165+
166+
/**
167+
* Get rows size
168+
*
169+
* @param Select $query
170+
* @return int
171+
*/
172+
private function getSize(Select $query)
173+
{
174+
$sql = $this->getSelectCountSql($query);
175+
$parentSelect = $this->getConnection()->select();
176+
$parentSelect->from(['core_select' => $sql]);
177+
$parentSelect->reset(Select::COLUMNS);
178+
$parentSelect->columns('COUNT(*)');
179+
$totalRecords = $this->getConnection()->fetchOne($parentSelect);
180+
181+
return (int) $totalRecords;
182+
}
183+
184+
/**
185+
* Reset limit and offset
186+
*
187+
* @param Select $query
188+
* @return Select
189+
*/
190+
private function getSelectCountSql(Select $query)
191+
{
192+
foreach ($this->countSqlSkipParts as $part => $toSkip) {
193+
if ($toSkip) {
194+
$query->reset($part);
195+
}
196+
}
197+
198+
return $query;
199+
}
156200
}

Helper/AdapterHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ public function getDocumentsFromAlgolia()
5050
$algoliaQuery = $query !== '__empty__' ? $query : '';
5151
$searchParams = [];
5252
$targetedIndex = null;
53-
if ($this->isReplaceCategory($storeId) || $this->isSearch() || $this->isLandingPage()) {
53+
54+
if ($this->isReplaceCategory() || $this->isSearch() || $this->isLandingPage()) {
5455
$searchParams = $this->getSearchParams($storeId);
5556

5657
// This is the first load of a landing page, so we have to get the parameters from the entity
57-
if ($this->isLandingPage() && is_null($this->filtersHelper->getRawQueryParameter())) {
58+
if ($this->isLandingPage() && $this->filtersHelper->getRawQueryParameter() === null) {
5859
$searchParams = array_merge(
5960
$searchParams,
6061
$this->filtersHelper->getLandingPageFilters($storeId)
6162
);
6263
$algoliaQuery = $this->filtersHelper->getLandingPageQuery();
6364
}
6465

65-
if (!is_null($this->filtersHelper->getRequest()->getParam('sortBy'))) {
66+
if ($this->filtersHelper->getRequest()->getParam('sortBy') !== null) {
6667
$targetedIndex = $this->filtersHelper->getRequest()->getParam('sortBy');
6768
}
6869
}

Helper/Data.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,17 @@ public function deleteObjects($storeId, $ids, $indexName)
8686
$this->algoliaHelper->deleteObjects($ids, $indexName);
8787
}
8888

89+
/**
90+
* @param string $query
91+
* @param int $storeId
92+
* @param array|null $searchParams
93+
* @param string|null $targetedIndex
94+
*
95+
* @return array
96+
*/
8997
public function getSearchResult($query, $storeId, $searchParams = null, $targetedIndex = null)
9098
{
91-
$indexName = !is_null($targetedIndex) ?
99+
$indexName = $targetedIndex !== null ?
92100
$targetedIndex :
93101
$this->getIndexName($this->productHelper->getIndexNameSuffix(), $storeId);
94102

@@ -126,7 +134,7 @@ public function getSearchResult($query, $storeId, $searchParams = null, $targete
126134
}
127135
}
128136

129-
return $data;
137+
return [$data, $answer['nbHits']];
130138
}
131139

132140
public function rebuildStoreAdditionalSectionsIndex($storeId)

Model/Indexer/Category.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,23 @@ public function executeRow($id)
101101
*/
102102
private function rebuildAffectedProducts($storeId)
103103
{
104-
$affectedProductsCount = count(self::$affectedProductIds);
104+
$affectedProducts = self::$affectedProductIds;
105+
$affectedProductsCount = count($affectedProducts);
106+
105107
if ($affectedProductsCount > 0 && $this->configHelper->indexProductOnCategoryProductsUpdate($storeId)) {
106-
/** @uses Data::rebuildStoreProductIndex() */
107-
$this->queue->addToQueue(
108-
Data::class,
109-
'rebuildStoreProductIndex',
110-
[
111-
'store_id' => $storeId,
112-
'product_ids' => self::$affectedProductIds,
113-
],
114-
$affectedProductsCount
115-
);
108+
$productsPerPage = $this->configHelper->getNumberOfElementByPage();
109+
foreach (array_chunk($affectedProducts, $productsPerPage) as $chunk) {
110+
/** @uses Data::rebuildStoreProductIndex() */
111+
$this->queue->addToQueue(
112+
Data::class,
113+
'rebuildStoreProductIndex',
114+
[
115+
'store_id' => $storeId,
116+
'product_ids' => $chunk,
117+
],
118+
count($chunk)
119+
);
120+
}
116121
}
117122
}
118123

Test/Integration/SearchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testSearch()
1717

1818
/** @var Data $helper */
1919
$helper = $this->getObjectManager()->create('Algolia\AlgoliaSearch\Helper\Data');
20-
$results = $helper->getSearchResult('', 1);
20+
list($results, $totalHits) = $helper->getSearchResult('', 1);
2121

2222
$this->assertNotEmpty($results);
2323
}

0 commit comments

Comments
 (0)