Browse Source

fix(server): Return correct FacetValues for search with grouped products

Michael Bromley 6 years ago
parent
commit
1919fff361

+ 45 - 0
server/e2e/default-search-plugin.e2e-spec.ts

@@ -1,3 +1,4 @@
+import gql from 'graphql-tag';
 import path from 'path';
 
 import {
@@ -117,6 +118,38 @@ describe('Default search plugin', () => {
         it('matches by facetId', () => testMatchFacetIds(shopClient));
 
         it('matches by collectionId', () => testMatchCollectionId(shopClient));
+
+        it('returns correct facetValues when not grouped by product', async () => {
+            const result = await shopClient.query(SEARCH_GET_FACET_VALUES, {
+                input: {
+                    groupByProduct: false,
+                },
+            });
+            expect(result.search.facetValues).toEqual([
+                { id: 'T_1', name: 'electronics' },
+                { id: 'T_2', name: 'computers' },
+                { id: 'T_3', name: 'photo' },
+                { id: 'T_4', name: 'sports equipment' },
+                { id: 'T_5', name: 'home & garden' },
+                { id: 'T_6', name: 'plants' },
+            ]);
+        });
+
+        it('returns correct facetValues when grouped by product', async () => {
+            const result = await shopClient.query(SEARCH_GET_FACET_VALUES, {
+                input: {
+                    groupByProduct: true,
+                },
+            });
+            expect(result.search.facetValues).toEqual([
+                { id: 'T_1', name: 'electronics' },
+                { id: 'T_2', name: 'computers' },
+                { id: 'T_3', name: 'photo' },
+                { id: 'T_4', name: 'sports equipment' },
+                { id: 'T_5', name: 'home & garden' },
+                { id: 'T_6', name: 'plants' },
+            ]);
+        });
     });
 
     describe('admin api', () => {
@@ -244,3 +277,15 @@ describe('Default search plugin', () => {
         });
     });
 });
+
+export const SEARCH_GET_FACET_VALUES = gql`
+    query SearchProducts($input: SearchInput!) {
+        search(input: $input) {
+            totalItems
+            facetValues {
+                id
+                name
+            }
+        }
+    }
+`;

+ 4 - 1
server/src/plugin/default-search-plugin/search-strategy/mysql-search-strategy.ts

@@ -23,7 +23,10 @@ export class MysqlSearchStrategy implements SearchStrategy {
             .createQueryBuilder('si')
             .select('GROUP_CONCAT(si.facetValueIds)', 'allFacetValues');
 
-        const facetValuesResult = await this.applyTermAndFilters(facetValuesQb, input).getRawOne();
+        const facetValuesResult = await this.applyTermAndFilters(facetValuesQb, {
+            ...input,
+            groupByProduct: false,
+        }).getRawOne();
         const allFacetValues = facetValuesResult ? facetValuesResult.allFacetValues || '' : '';
         return unique(allFacetValues.split(',').filter(x => x !== ''));
     }

+ 5 - 1
server/src/plugin/default-search-plugin/search-strategy/postgres-search-strategy.ts

@@ -23,7 +23,11 @@ export class PostgresSearchStrategy implements SearchStrategy {
             .createQueryBuilder('si')
             .select(`string_agg(si.facetValueIds,',')`, 'allFacetValues');
 
-        const facetValuesResult = await this.applyTermAndFilters(facetValuesQb, input, true).getRawOne();
+        const facetValuesResult = await this.applyTermAndFilters(
+            facetValuesQb,
+            { ...input, groupByProduct: false },
+            true,
+        ).getRawOne();
         const allFacetValues = facetValuesResult ? facetValuesResult.allFacetValues || '' : '';
         return unique(allFacetValues.split(',').filter(x => x !== ''));
     }

+ 4 - 1
server/src/plugin/default-search-plugin/search-strategy/sqlite-search-strategy.ts

@@ -24,7 +24,10 @@ export class SqliteSearchStrategy implements SearchStrategy {
             .createQueryBuilder('si')
             .select('GROUP_CONCAT(si.facetValueIds)', 'allFacetValues');
 
-        const facetValuesResult = await this.applyTermAndFilters(facetValuesQb, input).getRawOne();
+        const facetValuesResult = await this.applyTermAndFilters(facetValuesQb, {
+            ...input,
+            groupByProduct: false,
+        }).getRawOne();
         const allFacetValues = facetValuesResult ? facetValuesResult.allFacetValues || '' : '';
         return unique(allFacetValues.split(',').filter(x => x !== ''));
     }