Browse Source

fix(core): Fix inconsistencies in DefaultSearchPlugin search strategies

Michael Bromley 6 years ago
parent
commit
50fbae625f

+ 5 - 3
packages/core/src/plugin/default-search-plugin/search-strategy/mysql-search-strategy.ts

@@ -55,9 +55,6 @@ export class MysqlSearchStrategy implements SearchStrategy {
                 .addSelect('MAX(priceWithTax)', 'maxPriceWithTax');
         }
         this.applyTermAndFilters(ctx, qb, input);
-        if (input.term && input.term.length > this.minTermLength) {
-            qb.orderBy('score', 'DESC');
-        }
         if (sort) {
             if (sort.name) {
                 qb.addOrderBy('productName', sort.name);
@@ -65,6 +62,10 @@ export class MysqlSearchStrategy implements SearchStrategy {
             if (sort.price) {
                 qb.addOrderBy('price', sort.price);
             }
+        } else {
+            if (input.term && input.term.length > this.minTermLength) {
+                qb.orderBy('score', 'DESC');
+            }
         }
         if (enabledOnly) {
             qb.andWhere('si.enabled = :enabled', { enabled: true });
@@ -136,6 +137,7 @@ export class MysqlSearchStrategy implements SearchStrategy {
         qb.andWhere('channelId = :channelId', { channelId: ctx.channelId });
         if (input.groupByProduct === true) {
             qb.groupBy('productId');
+            qb.addSelect('BIT_OR(enabled)', 'productEnabled');
         }
         return qb;
     }

+ 6 - 3
packages/core/src/plugin/default-search-plugin/search-strategy/postgres-search-strategy.ts

@@ -58,9 +58,6 @@ export class PostgresSearchStrategy implements SearchStrategy {
                 .addSelect('MAX("priceWithTax")', 'maxPriceWithTax');
         }
         this.applyTermAndFilters(ctx, qb, input);
-        if (input.term && input.term.length > this.minTermLength) {
-            qb.orderBy('score', 'DESC');
-        }
 
         if (sort) {
             if (sort.name) {
@@ -69,6 +66,12 @@ export class PostgresSearchStrategy implements SearchStrategy {
             if (sort.price) {
                 qb.addOrderBy('"si_price"', sort.price);
             }
+        } else {
+            if (input.term && input.term.length > this.minTermLength) {
+                qb.addOrderBy('score', 'DESC');
+            } else {
+                qb.addOrderBy('"si_productVariantId"', 'ASC');
+            }
         }
         if (enabledOnly) {
             qb.andWhere('"si"."enabled" = :enabled', { enabled: true });

+ 3 - 1
packages/core/src/plugin/default-search-plugin/search-strategy/search-strategy-utils.ts

@@ -14,11 +14,13 @@ export function mapToSearchResult(raw: any, currencyCode: CurrencyCode): SearchR
         raw.minPriceWithTax !== undefined
             ? ({ min: raw.minPriceWithTax, max: raw.maxPriceWithTax } as PriceRange)
             : ({ value: raw.si_priceWithTax } as SinglePrice);
+
+    const enabled = raw.productEnabled != null ? !!Number(raw.productEnabled) : raw.si_enabled;
     return {
         sku: raw.si_sku,
         slug: raw.si_slug,
         price,
-        enabled: raw.si_enabled,
+        enabled,
         priceWithTax,
         currencyCode,
         productVariantId: raw.si_productVariantId,