Skip to content

Commit d27bd76

Browse files
committed
MAGE-1095: create BackendSearch Service to move getSearchResults method into
1 parent 357f689 commit d27bd76

File tree

3 files changed

+85
-65
lines changed

3 files changed

+85
-65
lines changed

Helper/Data.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Algolia\AlgoliaSearch\Service\Category\IndexBuilder as CategoryIndexBuilder;
1010
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1111
use Algolia\AlgoliaSearch\Service\Page\IndexBuilder as PageIndexBuilder;
12+
use Algolia\AlgoliaSearch\Service\Product\BackendSearch;
1213
use Algolia\AlgoliaSearch\Service\Product\IndexBuilder as ProductIndexBuilder;
1314
use Algolia\AlgoliaSearch\Service\Suggestion\IndexBuilder as SuggestionIndexBuilder;
1415
use Magento\Framework\Exception\LocalizedException;
@@ -28,6 +29,7 @@ public function __construct(
2829
protected AdditionalSectionIndexBuilder $additionalSectionIndexBuilder,
2930
protected PageIndexBuilder $pageIndexBuilder,
3031
protected SuggestionIndexBuilder $suggestionIndexBuilder,
32+
protected BackendSearch $backendSearch
3133
){}
3234

3335
/**
@@ -163,11 +165,11 @@ public function deleteInactiveProducts($storeId): void
163165
* @internal This method is currently unstable and should not be used. It may be revisited or fixed in a future version.
164166
*
165167
* @deprecated
166-
* Use Algolia\AlgoliaSearch\Service\Product\IndexBuilder::getSearchResult() instead
168+
* Use Algolia\AlgoliaSearch\Service\Product\BackendSearch::getSearchResult() instead
167169
*/
168170
public function getSearchResult(string $query, int $storeId, ?array $searchParams = null, ?string $targetedIndex = null): array
169171
{
170-
return $this->productIndexBuilder->getSearchResult($query, $storeId, $searchParams, $targetedIndex);
172+
return $this->backendSearch->getSearchResult($query, $storeId, $searchParams, $targetedIndex);
171173
}
172174

173175
/**

Service/Product/BackendSearch.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service\Product;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
7+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
8+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
11+
class BackendSearch
12+
{
13+
public function __construct(
14+
protected ConfigHelper $configHelper,
15+
protected ProductHelper $productHelper,
16+
protected AlgoliaHelper $algoliaHelper,
17+
){}
18+
19+
/**
20+
* @param string $query
21+
* @param int $storeId
22+
* @param array|null $searchParams
23+
* @param string|null $targetedIndex
24+
* @return array
25+
* @throws AlgoliaException|NoSuchEntityException
26+
* @internal This method is currently unstable and should not be used. It may be revisited or fixed in a future version.
27+
*
28+
*/
29+
public function getSearchResult(string $query, int $storeId, ?array $searchParams = null, ?string $targetedIndex = null): array
30+
{
31+
$indexName = $targetedIndex !== null ?
32+
$targetedIndex :
33+
$this->productHelper->getIndexName($storeId);
34+
35+
$numberOfResults = 1000;
36+
if ($this->configHelper->isInstantEnabled()) {
37+
$numberOfResults = min($this->configHelper->getNumberOfProductResults($storeId), 1000);
38+
}
39+
40+
$facetsToRetrieve = [];
41+
foreach ($this->configHelper->getFacets($storeId) as $facet) {
42+
$facetsToRetrieve[] = $facet['attribute'];
43+
}
44+
45+
$params = [
46+
'hitsPerPage' => $numberOfResults, // retrieve all the hits (hard limit is 1000)
47+
'attributesToRetrieve' => AlgoliaHelper::ALGOLIA_API_OBJECT_ID,
48+
'attributesToHighlight' => '',
49+
'attributesToSnippet' => '',
50+
'numericFilters' => ['visibility_search=1'],
51+
'removeWordsIfNoResults' => $this->configHelper->getRemoveWordsIfNoResult($storeId),
52+
'analyticsTags' => 'backend-search',
53+
'facets' => $facetsToRetrieve,
54+
'maxValuesPerFacet' => 100,
55+
];
56+
57+
if (is_array($searchParams)) {
58+
$params = array_merge($params, $searchParams);
59+
}
60+
61+
$response = $this->algoliaHelper->query($indexName, $query, $params);
62+
$answer = reset($response['results']);
63+
64+
$data = [];
65+
66+
foreach ($answer['hits'] as $i => $hit) {
67+
$productId = $hit[AlgoliaHelper::ALGOLIA_API_OBJECT_ID];
68+
69+
if ($productId) {
70+
$data[$productId] = [
71+
'entity_id' => $productId,
72+
'score' => $numberOfResults - $i,
73+
];
74+
}
75+
}
76+
77+
$facetsFromAnswer = $answer['facets'] ?? [];
78+
79+
return [$data, $answer['nbHits'], $facetsFromAnswer];
80+
}
81+
}

Service/Product/IndexBuilder.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -383,67 +383,4 @@ protected function checkPriceIndex(array $productIds): void
383383
$this->priceIndexer->reindexList($productIds);
384384
}
385385
}
386-
387-
/**
388-
* @param string $query
389-
* @param int $storeId
390-
* @param array|null $searchParams
391-
* @param string|null $targetedIndex
392-
* @return array
393-
* @throws AlgoliaException|NoSuchEntityException
394-
* @internal This method is currently unstable and should not be used. It may be revisited or fixed in a future version.
395-
*
396-
*/
397-
public function getSearchResult(string $query, int $storeId, ?array $searchParams = null, ?string $targetedIndex = null): array
398-
{
399-
$indexName = $targetedIndex !== null ?
400-
$targetedIndex :
401-
$this->productHelper->getIndexName($storeId);
402-
403-
$numberOfResults = 1000;
404-
if ($this->configHelper->isInstantEnabled()) {
405-
$numberOfResults = min($this->configHelper->getNumberOfProductResults($storeId), 1000);
406-
}
407-
408-
$facetsToRetrieve = [];
409-
foreach ($this->configHelper->getFacets($storeId) as $facet) {
410-
$facetsToRetrieve[] = $facet['attribute'];
411-
}
412-
413-
$params = [
414-
'hitsPerPage' => $numberOfResults, // retrieve all the hits (hard limit is 1000)
415-
'attributesToRetrieve' => AlgoliaHelper::ALGOLIA_API_OBJECT_ID,
416-
'attributesToHighlight' => '',
417-
'attributesToSnippet' => '',
418-
'numericFilters' => ['visibility_search=1'],
419-
'removeWordsIfNoResults' => $this->configHelper->getRemoveWordsIfNoResult($storeId),
420-
'analyticsTags' => 'backend-search',
421-
'facets' => $facetsToRetrieve,
422-
'maxValuesPerFacet' => 100,
423-
];
424-
425-
if (is_array($searchParams)) {
426-
$params = array_merge($params, $searchParams);
427-
}
428-
429-
$response = $this->algoliaHelper->query($indexName, $query, $params);
430-
$answer = reset($response['results']);
431-
432-
$data = [];
433-
434-
foreach ($answer['hits'] as $i => $hit) {
435-
$productId = $hit[AlgoliaHelper::ALGOLIA_API_OBJECT_ID];
436-
437-
if ($productId) {
438-
$data[$productId] = [
439-
'entity_id' => $productId,
440-
'score' => $numberOfResults - $i,
441-
];
442-
}
443-
}
444-
445-
$facetsFromAnswer = $answer['facets'] ?? [];
446-
447-
return [$data, $answer['nbHits'], $facetsFromAnswer];
448-
}
449386
}

0 commit comments

Comments
 (0)