Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix api facets #258

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Api/Resolver/FacetsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function resolve(array $data): array
{
$this->autoDiscoverRegistry->autoRegister();

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

foreach ($data['facets'] ?? [] as $facetId => $selectedBuckets) {
if (!$selectedBuckets) {
continue;
}

$facet = $this->facetRegistry->getFacetById($facetId);
$boolQuery->addFilter($facet->getQuery($selectedBuckets));
}

$facets = $this->finder->findPaginated($query);
$adapter = $facets->getAdapter();
if (!$adapter instanceof FantaPaginatorAdapter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Sylius\Component\Core\Model\Channel:
channel_web:
code: 'WEB'
name: 'Web Channel'
hostname: 'localhost'
description: 'Lorem ipsum'
baseCurrency: '@currency_usd'
defaultLocale: '@locale_en'
locales: ['@locale_en']
color: 'black'
enabled: true
taxCalculationStrategy: 'order_items_based'

Sylius\Component\Currency\Model\Currency:
currency_usd:
code: 'USD'

Sylius\Component\Locale\Model\Locale:
locale_en:
code: 'en_US'

Sylius\Component\Core\Model\Product:
product_mug:
code: 'MUG'
channels: ['@channel_web']
currentLocale: 'en_US'
translations:
en_US: '@product_translation_mug'
product_mug2:
code: 'MUG2'
channels: ['@channel_web']
currentLocale: 'en_US'
translations:
en_US: '@product_translation_mug2'
product_mug3:
code: 'MUG3'
channels: ['@channel_web']
currentLocale: 'en_US'
translations:
en_US: '@product_translation_mug3'

Sylius\Component\Core\Model\ProductTranslation:
product_translation_mug:
slug: 'mug'
locale: 'en_US'
name: 'Mug'
description: '<paragraph(2)>'
translatable: '@product_mug'
product_translation_mug2:
slug: 'mug-2'
locale: 'en_US'
name: 'Mug 2'
description: '<paragraph(2)>'
translatable: '@product_mug2'
product_translation_mug3:
slug: 'mug-3'
locale: 'en_US'
name: 'Mug 3'
description: '<paragraph(2)>'
translatable: '@product_mug3'

Sylius\Component\Product\Model\ProductAttributeTranslation:
attributeTranslation1:
locale: en_US
name: "Product color"
translatable: "@product_attribute_color"
attributeTranslation2:
locale: en_US
name: "Product material"
translatable: "@product_attribute_material"

Sylius\Component\Product\Model\ProductAttribute:
product_attribute_color:
code: 'color'
type: 'text'
storage_type: 'text'
position: 1
translatable: 1
product_attribute_material:
code: 'material'
type: 'text'
storage_type: 'text'
position: 1
translatable: 1

Sylius\Component\Product\Model\ProductAttributeValue:
product_attribute_value_color_1:
product: '@product_mug'
attribute: '@product_attribute_color'
localeCode: 'en_US'
value: 'red'
product_attribute_value_color_2:
product: '@product_mug2'
attribute: '@product_attribute_color'
localeCode: 'en_US'
value: 'blue'
product_attribute_value_color_3:
product: '@product_mug3'
attribute: '@product_attribute_color'
localeCode: 'en_US'
value: 'white'
product_attribute_value_material_1:
product: '@product_mug'
attribute: '@product_attribute_material'
localeCode: 'en_US'
value: 'ceramic'
product_attribute_value_material_2:
product: '@product_mug2'
attribute: '@product_attribute_material'
localeCode: 'en_US'
value: 'ceramic'

Sylius\Component\Core\Model\Taxon:
mugs:
code: "mugs"

Sylius\Component\Core\Model\ProductTaxon:
productTaxon1:
product: "@product_mug"
taxon: "@mugs"
position: 0
14 changes: 14 additions & 0 deletions tests/PHPUnit/Integration/Api/ProductListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public function test_it_finds_products_by_name_and_multiple_facets(): void
$this->assertResponse($response, 'test_it_finds_products_by_name_and_multiple_facets', Response::HTTP_OK);
}

public function test_it_updates_facets(): void
{
$this->loadFixturesFromFiles(['test_it_updates_facets.yaml']);
$this->populateElasticsearch();

$this->client->request(
'GET',
'/api/v2/shop/products/search?query=mug&facets[color][]=red&facets[material][]=ceramic'
);

$response = $this->client->getResponse();
$this->assertResponse($response, 'test_it_updates_facets', Response::HTTP_OK);
}

private function populateElasticsearch(): void
{
$process = new Process(['tests/Application/bin/console', 'fos:elastica:populate']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"items": [
{
"productTaxons": [
"/api/v2/shop/product-taxons/@string@"
],
"mainTaxon": null,
"averageRating": 0,
"images": [],
"id": "@integer@",
"code": "MUG",
"variants": [],
"options": [],
"associations": [],
"createdAt": "@string@",
"updatedAt": "@string@",
"shortDescription": null,
"reviews": [],
"name": "Mug",
"description": "@string@",
"slug": "mug"
}
],
"facets": {
"material": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ceramic",
"doc_count": 1
}
]
},
"color": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 1
}
]
},
"taxon": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "mugs",
"doc_count": 1
}
]
}
},
"pagination": {
"current_page": 1,
"has_previous_page": false,
"has_next_page": false,
"per_page": 9,
"total_items": 1,
"total_pages": 1
}
}
Loading