Browse Source

feature(elasticsearch-plugin): adds search options by collection slugs or collection IDs (#3182)

Co-authored-by: David Höck <david@vendure.io>
Alexis Vigoureux 2 months ago
parent
commit
6b2e0e10a8

+ 2 - 0
packages/core/src/api/schema/common/common-types.graphql

@@ -178,7 +178,9 @@ input SearchInput {
     facetValueOperator: LogicalOperator @deprecated(reason: "Use `facetValueFilters` instead")
     facetValueFilters: [FacetValueFilterInput!]
     collectionId: ID
+    collectionIds: [ID!]
     collectionSlug: String
+    collectionSlugs: [String!]
     groupByProduct: Boolean
     take: Int
     skip: Int

+ 23 - 0
packages/elasticsearch-plugin/src/build-elastic-body.spec.ts

@@ -246,6 +246,15 @@ describe('buildElasticBody()', () => {
         });
     });
 
+    it('collectionIds', () => {
+        const result = buildElasticBody({ collectionIds: ['1', '2'] }, searchConfig, CHANNEL_ID, LanguageCode.en);
+        expect(result.query).toEqual({
+            bool: {
+                filter: [CHANNEL_ID_TERM, LANGUAGE_CODE_TERM, { terms: { collectionIds: ['1', '2'] } }],
+            },
+        });
+    });
+
     it('collectionSlug', () => {
         const result = buildElasticBody(
             { collectionSlug: 'plants' },
@@ -262,6 +271,20 @@ describe('buildElasticBody()', () => {
         });
     });
 
+    it('collectionSlugs', () => {
+        const result = buildElasticBody(
+            { collectionSlugs: ['plants', 'tree'] },
+            searchConfig,
+            CHANNEL_ID,
+            LanguageCode.en,
+        );
+        expect(result.query).toEqual({
+            bool: {
+                filter: [CHANNEL_ID_TERM, LANGUAGE_CODE_TERM, { terms: { collectionSlugs: ['plants', 'tree'] } }],
+            },
+        });
+    });
+
     it('paging', () => {
         const result = buildElasticBody(
             { skip: 20, take: 10 },

+ 10 - 0
packages/elasticsearch-plugin/src/build-elastic-body.ts

@@ -20,7 +20,9 @@ export function buildElasticBody(
         facetValueIds,
         facetValueOperator,
         collectionId,
+        collectionIds,
         collectionSlug,
+        collectionSlugs,
         groupByProduct,
         groupBySKU,
         skip,
@@ -86,10 +88,18 @@ export function buildElasticBody(
         ensureBoolFilterExists(query);
         query.bool.filter.push({ term: { collectionIds: collectionId } });
     }
+    if (collectionIds) {
+        ensureBoolFilterExists(query)
+        query.bool.filter.push({ terms: { collectionIds: Array.from(new Set(collectionIds)) } })
+    }
     if (collectionSlug) {
         ensureBoolFilterExists(query);
         query.bool.filter.push({ term: { collectionSlugs: collectionSlug } });
     }
+    if (collectionSlugs) {
+        ensureBoolFilterExists(query)
+        query.bool.filter.push({ terms: { collectionSlugs: Array.from(new Set(collectionSlugs)) } })
+    }
     if (enabledOnly) {
         ensureBoolFilterExists(query);
         query.bool.filter.push({ term: { enabled: true } });