Skip to content

Commit dca1463

Browse files
Merge pull request #258 from BitBagCommerce/api-facets-fix
Fix api facets
2 parents 050906f + 300c360 commit dca1463

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

src/Api/Resolver/FacetsResolver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function resolve(array $data): array
3333
{
3434
$this->autoDiscoverRegistry->autoRegister();
3535

36+
/** @var Query\BoolQuery $boolQuery */
3637
$boolQuery = $this->queryBuilder->buildQuery($data);
3738
$query = new Query($boolQuery);
3839
$query->setSize(0);
@@ -41,6 +42,15 @@ public function resolve(array $data): array
4142
$query->addAggregation($facet->getAggregation()->setName($facetId));
4243
}
4344

45+
foreach ($data['facets'] ?? [] as $facetId => $selectedBuckets) {
46+
if (!$selectedBuckets) {
47+
continue;
48+
}
49+
50+
$facet = $this->facetRegistry->getFacetById($facetId);
51+
$boolQuery->addFilter($facet->getQuery($selectedBuckets));
52+
}
53+
4454
$facets = $this->finder->findPaginated($query);
4555
$adapter = $facets->getAdapter();
4656
if (!$adapter instanceof FantaPaginatorAdapter) {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Sylius\Component\Core\Model\Channel:
2+
channel_web:
3+
code: 'WEB'
4+
name: 'Web Channel'
5+
hostname: 'localhost'
6+
description: 'Lorem ipsum'
7+
baseCurrency: '@currency_usd'
8+
defaultLocale: '@locale_en'
9+
locales: ['@locale_en']
10+
color: 'black'
11+
enabled: true
12+
taxCalculationStrategy: 'order_items_based'
13+
14+
Sylius\Component\Currency\Model\Currency:
15+
currency_usd:
16+
code: 'USD'
17+
18+
Sylius\Component\Locale\Model\Locale:
19+
locale_en:
20+
code: 'en_US'
21+
22+
Sylius\Component\Core\Model\Product:
23+
product_mug:
24+
code: 'MUG'
25+
channels: ['@channel_web']
26+
currentLocale: 'en_US'
27+
translations:
28+
en_US: '@product_translation_mug'
29+
product_mug2:
30+
code: 'MUG2'
31+
channels: ['@channel_web']
32+
currentLocale: 'en_US'
33+
translations:
34+
en_US: '@product_translation_mug2'
35+
product_mug3:
36+
code: 'MUG3'
37+
channels: ['@channel_web']
38+
currentLocale: 'en_US'
39+
translations:
40+
en_US: '@product_translation_mug3'
41+
42+
Sylius\Component\Core\Model\ProductTranslation:
43+
product_translation_mug:
44+
slug: 'mug'
45+
locale: 'en_US'
46+
name: 'Mug'
47+
description: '<paragraph(2)>'
48+
translatable: '@product_mug'
49+
product_translation_mug2:
50+
slug: 'mug-2'
51+
locale: 'en_US'
52+
name: 'Mug 2'
53+
description: '<paragraph(2)>'
54+
translatable: '@product_mug2'
55+
product_translation_mug3:
56+
slug: 'mug-3'
57+
locale: 'en_US'
58+
name: 'Mug 3'
59+
description: '<paragraph(2)>'
60+
translatable: '@product_mug3'
61+
62+
Sylius\Component\Product\Model\ProductAttributeTranslation:
63+
attributeTranslation1:
64+
locale: en_US
65+
name: "Product color"
66+
translatable: "@product_attribute_color"
67+
attributeTranslation2:
68+
locale: en_US
69+
name: "Product material"
70+
translatable: "@product_attribute_material"
71+
72+
Sylius\Component\Product\Model\ProductAttribute:
73+
product_attribute_color:
74+
code: 'color'
75+
type: 'text'
76+
storage_type: 'text'
77+
position: 1
78+
translatable: 1
79+
product_attribute_material:
80+
code: 'material'
81+
type: 'text'
82+
storage_type: 'text'
83+
position: 1
84+
translatable: 1
85+
86+
Sylius\Component\Product\Model\ProductAttributeValue:
87+
product_attribute_value_color_1:
88+
product: '@product_mug'
89+
attribute: '@product_attribute_color'
90+
localeCode: 'en_US'
91+
value: 'red'
92+
product_attribute_value_color_2:
93+
product: '@product_mug2'
94+
attribute: '@product_attribute_color'
95+
localeCode: 'en_US'
96+
value: 'blue'
97+
product_attribute_value_color_3:
98+
product: '@product_mug3'
99+
attribute: '@product_attribute_color'
100+
localeCode: 'en_US'
101+
value: 'white'
102+
product_attribute_value_material_1:
103+
product: '@product_mug'
104+
attribute: '@product_attribute_material'
105+
localeCode: 'en_US'
106+
value: 'ceramic'
107+
product_attribute_value_material_2:
108+
product: '@product_mug2'
109+
attribute: '@product_attribute_material'
110+
localeCode: 'en_US'
111+
value: 'ceramic'
112+
113+
Sylius\Component\Core\Model\Taxon:
114+
mugs:
115+
code: "mugs"
116+
117+
Sylius\Component\Core\Model\ProductTaxon:
118+
productTaxon1:
119+
product: "@product_mug"
120+
taxon: "@mugs"
121+
position: 0

tests/PHPUnit/Integration/Api/ProductListingTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ public function test_it_finds_products_by_name_and_multiple_facets(): void
7171
$this->assertResponse($response, 'test_it_finds_products_by_name_and_multiple_facets', Response::HTTP_OK);
7272
}
7373

74+
public function test_it_updates_facets(): void
75+
{
76+
$this->loadFixturesFromFiles(['test_it_updates_facets.yaml']);
77+
$this->populateElasticsearch();
78+
79+
$this->client->request(
80+
'GET',
81+
'/api/v2/shop/products/search?query=mug&facets[color][]=red&facets[material][]=ceramic'
82+
);
83+
84+
$response = $this->client->getResponse();
85+
$this->assertResponse($response, 'test_it_updates_facets', Response::HTTP_OK);
86+
}
87+
7488
private function populateElasticsearch(): void
7589
{
7690
$process = new Process(['tests/Application/bin/console', 'fos:elastica:populate']);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"items": [
3+
{
4+
"productTaxons": [
5+
"/api/v2/shop/product-taxons/@string@"
6+
],
7+
"mainTaxon": null,
8+
"averageRating": 0,
9+
"images": [],
10+
"id": "@integer@",
11+
"code": "MUG",
12+
"variants": [],
13+
"options": [],
14+
"associations": [],
15+
"createdAt": "@string@",
16+
"updatedAt": "@string@",
17+
"shortDescription": null,
18+
"reviews": [],
19+
"name": "Mug",
20+
"description": "@string@",
21+
"slug": "mug"
22+
}
23+
],
24+
"facets": {
25+
"material": {
26+
"doc_count_error_upper_bound": 0,
27+
"sum_other_doc_count": 0,
28+
"buckets": [
29+
{
30+
"key": "ceramic",
31+
"doc_count": 1
32+
}
33+
]
34+
},
35+
"color": {
36+
"doc_count_error_upper_bound": 0,
37+
"sum_other_doc_count": 0,
38+
"buckets": [
39+
{
40+
"key": "red",
41+
"doc_count": 1
42+
}
43+
]
44+
},
45+
"taxon": {
46+
"doc_count_error_upper_bound": 0,
47+
"sum_other_doc_count": 0,
48+
"buckets": [
49+
{
50+
"key": "mugs",
51+
"doc_count": 1
52+
}
53+
]
54+
}
55+
},
56+
"pagination": {
57+
"current_page": 1,
58+
"has_previous_page": false,
59+
"has_next_page": false,
60+
"per_page": 9,
61+
"total_items": 1,
62+
"total_pages": 1
63+
}
64+
}

0 commit comments

Comments
 (0)