Skip to content

Commit 4329227

Browse files
authored
Merge pull request #137 from AzizKHAN030/m244_catalog-graphql
Passed the 6th param $includeDirectChildrenOnly class
2 parents a0521aa + b0aace9 commit 4329227

File tree

5 files changed

+249
-7
lines changed

5 files changed

+249
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace ScandiPWA\CatalogGraphQl\Elasticsearch5\SearchAdapter\Aggregation;
9+
10+
use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
11+
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
12+
use Magento\Elasticsearch\Model\Config;
13+
use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
14+
use Magento\CatalogSearch\Model\Indexer\Fulltext;
15+
use Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval as CoreInterval;
16+
17+
/**
18+
* Aggregate price intervals for search query result.
19+
*/
20+
class Interval extends CoreInterval
21+
{
22+
/**
23+
* Minimal possible value
24+
*/
25+
const DELTA = 0.005;
26+
27+
/**
28+
* @var ConnectionManager
29+
*/
30+
protected $connectionManager;
31+
32+
/**
33+
* @var FieldMapperInterface
34+
*/
35+
protected $fieldMapper;
36+
37+
/**
38+
* @var Config
39+
*/
40+
protected $clientConfig;
41+
42+
/**
43+
* @var string
44+
*/
45+
protected $fieldName;
46+
47+
/**
48+
* @var string
49+
*/
50+
protected $storeId;
51+
52+
/**
53+
* @var array
54+
*/
55+
protected $entityIds;
56+
57+
/**
58+
* @var SearchIndexNameResolver
59+
*/
60+
protected $searchIndexNameResolver;
61+
62+
/**
63+
* @param ConnectionManager $connectionManager
64+
* @param FieldMapperInterface $fieldMapper
65+
* @param Config $clientConfig
66+
* @param SearchIndexNameResolver $searchIndexNameResolver
67+
* @param string $fieldName
68+
* @param string $storeId
69+
* @param array $entityIds
70+
*/
71+
public function __construct(
72+
ConnectionManager $connectionManager,
73+
FieldMapperInterface $fieldMapper,
74+
Config $clientConfig,
75+
SearchIndexNameResolver $searchIndexNameResolver,
76+
string $fieldName,
77+
string $storeId,
78+
array $entityIds
79+
) {
80+
$this->connectionManager = $connectionManager;
81+
$this->fieldMapper = $fieldMapper;
82+
$this->clientConfig = $clientConfig;
83+
$this->fieldName = $fieldName;
84+
$this->storeId = $storeId;
85+
$this->entityIds = $entityIds;
86+
$this->searchIndexNameResolver = $searchIndexNameResolver;
87+
88+
parent::__construct(
89+
$connectionManager,
90+
$fieldMapper,
91+
$clientConfig,
92+
$searchIndexNameResolver,
93+
$fieldName,
94+
$storeId,
95+
$entityIds,
96+
97+
);
98+
}
99+
100+
/**
101+
* @inheritdoc
102+
*/
103+
public function load($limit, $offset = null, $lower = null, $upper = null)
104+
{
105+
106+
$from = ['gte' => 0]; //Added this because in some situations the $lower is null and $from is not declared
107+
$to = ['lt' => 0]; //Added this because in some situations the $data is null and $to is not declared
108+
109+
if ($lower) {
110+
$from = ['gte' => $lower - self::DELTA];
111+
}
112+
113+
if ($upper) {
114+
$to = ['lt' => $upper - self::DELTA];
115+
}
116+
117+
$requestQuery = $this->prepareBaseRequestQuery($from, $to);
118+
$requestQuery = array_merge_recursive(
119+
$requestQuery,
120+
['body' => ['stored_fields' => [$this->fieldName], 'size' => $limit]]
121+
);
122+
123+
if ($offset) {
124+
$requestQuery['body']['from'] = $offset;
125+
}
126+
127+
$queryResult = $this->connectionManager->getConnection()
128+
->query($requestQuery);
129+
130+
return $this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName);
131+
}
132+
133+
134+
/**
135+
* @inheritdoc
136+
*/
137+
public function loadPrevious($data, $index, $lower = null)
138+
{
139+
140+
$from = ['gte' => 0]; //Added this because in some situations the $lower is null and $from is not declared
141+
$to = ['lt' => 0]; //Added this because in some situations the $data is null and $to is not declared
142+
143+
if ($lower) {
144+
$from = ['gte' => $lower - self::DELTA];
145+
}
146+
if ($data) {
147+
$to = ['lt' => $data - self::DELTA];
148+
}
149+
150+
$requestQuery = $this->prepareBaseRequestQuery($from, $to);
151+
$requestQuery = array_merge_recursive(
152+
$requestQuery,
153+
['size' => 0]
154+
);
155+
156+
$queryResult = $this->connectionManager->getConnection()
157+
->query($requestQuery);
158+
159+
$offset = $queryResult['hits']['total'];
160+
if (!$offset) {
161+
return false;
162+
}
163+
164+
if (is_array($offset)) {
165+
$offset = $offset['value'];
166+
}
167+
168+
return $this->load($index - $offset + 1, $offset - 1, $lower);
169+
}
170+
171+
/**
172+
* Conver array values to float type.
173+
*
174+
* @param array $hits
175+
* @param string $fieldName
176+
*
177+
* @return float[]
178+
*/
179+
protected function arrayValuesToFloat(array $hits, string $fieldName): array
180+
{
181+
$returnPrices = [];
182+
foreach ($hits as $hit) {
183+
$returnPrices[] = (float)$hit['fields'][$fieldName][0];
184+
}
185+
186+
return $returnPrices;
187+
}
188+
189+
/**
190+
* Prepare base query for search.
191+
*
192+
* @param array|null $from
193+
* @param array|null $to
194+
* @return array
195+
*/
196+
protected function prepareBaseRequestQuery($from = null, $to = null): array
197+
{
198+
$requestQuery = [
199+
'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
200+
'type' => $this->clientConfig->getEntityType(),
201+
'body' => [
202+
'stored_fields' => [
203+
'_id',
204+
],
205+
'query' => [
206+
'bool' => [
207+
'must' => [
208+
'match_all' => new \stdClass(),
209+
],
210+
'filter' => [
211+
'bool' => [
212+
'must' => [
213+
[
214+
'terms' => [
215+
'_id' => $this->entityIds,
216+
],
217+
],
218+
[
219+
'range' => [
220+
$this->fieldName => array_merge($from, $to),
221+
],
222+
],
223+
],
224+
],
225+
],
226+
],
227+
],
228+
'sort' => [
229+
$this->fieldName,
230+
],
231+
],
232+
];
233+
234+
return $requestQuery;
235+
}
236+
}

src/Model/Layer/Filter/Category.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Magento\CatalogGraphQl\DataProvider\CategoryAttributesMapper;
1717
use Magento\CatalogGraphQl\DataProvider\Category\Query\CategoryAttributeQuery;
18+
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category\IncludeDirectChildrenOnly;
1819
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\RootCategoryProvider;
1920
use Magento\Framework\Api\Search\AggregationInterface;
2021
use Magento\Framework\App\ResourceConnection;
@@ -40,21 +41,24 @@ class Category extends OriginalCategoryBuilder
4041
* @param RootCategoryProvider $rootCategoryProvider
4142
* @param ResourceConnection $resourceConnection
4243
* @param LayerFormatter $layerFormatter
44+
* @param IncludeDirectChildrenOnly $includeDirectChildrenOnly
4345
*/
4446
public function __construct(
4547
CategoryAttributeQuery $categoryAttributeQuery,
4648
CategoryAttributesMapper $attributesMapper,
4749
RootCategoryProvider $rootCategoryProvider,
4850
ResourceConnection $resourceConnection,
4951
LayerFormatter $layerFormatter,
52+
IncludeDirectChildrenOnly $includeDirectChildrenOnly,
5053
AttributeDataProvider $attributeDataProvider
5154
) {
5255
parent::__construct(
5356
$categoryAttributeQuery,
5457
$attributesMapper,
5558
$rootCategoryProvider,
5659
$resourceConnection,
57-
$layerFormatter
60+
$layerFormatter,
61+
$includeDirectChildrenOnly
5862
);
5963

6064
$this->attributeDataProvider = $attributeDataProvider;

src/Model/Resolver/Category/SortFields.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private function getDefaultSortOption($context): string {
115115
private function getSortOptionsByCategory(int $categoryId): array {
116116
$result = [];
117117
$category = $this->categoryRepository->get($categoryId);
118-
$sortBy = $category->getAvailableSortBy();
118+
$sortBy = $category->getAvailableSortBy() ?? [];
119119

120120
foreach ($sortBy as $sortItem) {
121121
$result[] = [

src/SearchAdapter/Query/Builder/Match.php src/SearchAdapter/Query/Builder/MatchQuery.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
use Magento\Elasticsearch\Model\Config;
1616
use Magento\Elasticsearch\SearchAdapter\Query\ValueTransformerPool;
1717
use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
18-
use Magento\Elasticsearch\SearchAdapter\Query\Builder\Match as CoreMatch;
18+
use Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery as CoreMatch;
1919

2020
/**
21-
* Class Match
21+
* Class MatchQuery
2222
* @package ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder
2323
*/
24-
class Match extends CoreMatch
24+
class MatchQuery extends CoreMatch
2525
{
2626
/**
2727
* Define fuzziness level of search query

src/etc/di.xml

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
<preference for="Magento\GroupedProductGraphQl\Model\Resolver\GroupedItems"
6666
type="ScandiPWA\CatalogGraphQl\Model\Resolver\GroupedItems" />
6767

68-
<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\Match"
69-
type="ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder\Match"/>
68+
<preference for="Magento\Elasticsearch\SearchAdapter\Query\Builder\MatchQuery"
69+
type="ScandiPWA\CatalogGraphQl\SearchAdapter\Query\Builder\MatchQuery"/>
7070

7171
<type name="Magento\Framework\GraphQl\Query\QueryComplexityLimiter">
7272
<arguments>
@@ -105,4 +105,6 @@
105105
<plugin name="aggregation_equalize_products_count_handler"
106106
type="ScandiPWA\CatalogGraphQl\Plugin\SearchAdapter\Aggregation\BuilderPlugin" />
107107
</type>
108+
<preference for="Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Aggregation\Interval"
109+
type="ScandiPWA\CatalogGraphQl\Elasticsearch5\SearchAdapter\Aggregation\Interval" />
108110
</config>

0 commit comments

Comments
 (0)