From eea60c3e09981d6f12d10ada1b2847f675fdc48d Mon Sep 17 00:00:00 2001 From: Simon Walenkamp Hansen Date: Wed, 8 Jan 2025 11:59:23 +0100 Subject: [PATCH 1/3] Feat: add support for search hightlight shape snippet settings --- .../src/builders/search/productHighlightingBuilder.ts | 5 +++-- .../integration-tests/productSearch.integration.test.ts | 4 ++-- .../builders/search/productSearchBuilder.unit.test.ts | 6 +++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/client/src/builders/search/productHighlightingBuilder.ts b/packages/client/src/builders/search/productHighlightingBuilder.ts index e8ee7f5..5e2d6ab 100644 --- a/packages/client/src/builders/search/productHighlightingBuilder.ts +++ b/packages/client/src/builders/search/productHighlightingBuilder.ts @@ -1,4 +1,4 @@ -import { HighlightSettings2ProductProductHighlightPropsHighlightSettings2Limits, HighlightSettings2ProductProductHighlightPropsHighlightSettings2ResponseShape, ProductHighlightProps, ProductSearchSettingsHighlightSettings } from '../../models/data-contracts'; +import { HighlightSettings2ProductProductHighlightPropsHighlightSettings2Limits, HighlightSettings2ProductProductHighlightPropsHighlightSettings2ResponseShape, HighlightSettings2ProductProductHighlightPropsHighlightSettings2TextSnippetsSettings, ProductHighlightProps, ProductSearchSettingsHighlightSettings } from '../../models/data-contracts'; export class ProductHighlightingBuilder { private enabledState: boolean = true; @@ -32,8 +32,9 @@ export class ProductHighlightingBuilder { return this; } - public setShape(shape: { includeOffsets: boolean }): this { + public setShape(shape: { includeOffsets: boolean, textSnippets?: HighlightSettings2ProductProductHighlightPropsHighlightSettings2TextSnippetsSettings }): this { this.shape.includeOffsets = shape.includeOffsets; + this.shape.textSnippets = shape.textSnippets; return this; } diff --git a/packages/client/tests/integration-tests/productSearch.integration.test.ts b/packages/client/tests/integration-tests/productSearch.integration.test.ts index c751a61..5c33bdd 100644 --- a/packages/client/tests/integration-tests/productSearch.integration.test.ts +++ b/packages/client/tests/integration-tests/productSearch.integration.test.ts @@ -102,10 +102,10 @@ test('Highlighting', async() => { h.setHighlightable({ dataKeys: ['Description'] }) // You have to specify to include the offset. // Currently offset is the only way to get a result, so if not set, you won't get a result. - h.setShape({ includeOffsets: true }) + h.setShape({ includeOffsets: true, textSnippets: { includeTextSnippets: true } }) }).build(); - const result = await searcher.searchProducts(request); expect(result?.results![0].highlight?.offsets?.data[0].value.length).toBeGreaterThan(0); + expect(result?.results![0].highlight?.snippets?.data[0].value[0]).toBe("...last word is highlighted"); }) diff --git a/packages/client/tests/unit-tests/builders/search/productSearchBuilder.unit.test.ts b/packages/client/tests/unit-tests/builders/search/productSearchBuilder.unit.test.ts index 3cfa9a2..40f7a9e 100644 --- a/packages/client/tests/unit-tests/builders/search/productSearchBuilder.unit.test.ts +++ b/packages/client/tests/unit-tests/builders/search/productSearchBuilder.unit.test.ts @@ -120,7 +120,10 @@ test('searchHightlighting', () => { maxSnippetsPerField: 3 }); h.setShape({ - includeOffsets: true + includeOffsets: true, + textSnippets: { + includeTextSnippets: true, + } }); }).build(); @@ -132,6 +135,7 @@ test('searchHightlighting', () => { expect(subject.settings?.highlight?.limit.maxSnippetsPerEntry).toBe(2); expect(subject.settings?.highlight?.limit.maxSnippetsPerField).toBe(3); expect(subject.settings?.highlight?.shape.includeOffsets).toBe(true); + expect(subject.settings?.highlight?.shape.textSnippets?.includeTextSnippets).toBe(true); }); test('includeDisabled in ProductHasVariantsFilter', () => { From b006656c78f7afa7fecf2a1746972b1fe9f8d52c Mon Sep 17 00:00:00 2001 From: Simon Walenkamp Hansen Date: Thu, 9 Jan 2025 12:01:25 +0100 Subject: [PATCH 2/3] Content --- .../src/builders/search/contentHighlightingBuilder.ts | 6 ++++-- .../integration-tests/contentSearch.integration.test.ts | 3 ++- .../builders/search/contentSearchBuilder.unit.test.ts | 6 +++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/client/src/builders/search/contentHighlightingBuilder.ts b/packages/client/src/builders/search/contentHighlightingBuilder.ts index 0dc468b..1f161be 100644 --- a/packages/client/src/builders/search/contentHighlightingBuilder.ts +++ b/packages/client/src/builders/search/contentHighlightingBuilder.ts @@ -1,4 +1,4 @@ -import { ContentHighlightProps, ContentSearchSettingsHighlightSettings, HighlightSettings2ContentContentHighlightPropsHighlightSettings2Limits, HighlightSettings2ContentContentHighlightPropsHighlightSettings2ResponseShape } from '../../models/data-contracts'; +import { ContentHighlightProps, ContentSearchSettingsHighlightSettings, HighlightSettings2ContentContentHighlightPropsHighlightSettings2Limits, HighlightSettings2ContentContentHighlightPropsHighlightSettings2ResponseShape, HighlightSettings2ContentContentHighlightPropsHighlightSettings2TextSnippetsSettings } from '../../models/data-contracts'; export class ContentHighlightingBuilder { private enabledState: boolean = true; @@ -32,12 +32,14 @@ export class ContentHighlightingBuilder { return this; } - public setShape(shape: { includeOffsets: boolean }): this { + public setShape(shape: { includeOffsets: boolean, textSnippets?: HighlightSettings2ContentContentHighlightPropsHighlightSettings2TextSnippetsSettings }): this { this.shape.includeOffsets = shape.includeOffsets; + this.shape.textSnippets = shape.textSnippets; return this; } + public build(): ContentSearchSettingsHighlightSettings { return { $type: 'Relewise.Client.Requests.Search.Settings.ContentSearchSettings+HighlightSettings, Relewise.Client', diff --git a/packages/client/tests/integration-tests/contentSearch.integration.test.ts b/packages/client/tests/integration-tests/contentSearch.integration.test.ts index d113f74..9f8a2c7 100644 --- a/packages/client/tests/integration-tests/contentSearch.integration.test.ts +++ b/packages/client/tests/integration-tests/contentSearch.integration.test.ts @@ -44,10 +44,11 @@ test('Highlighting', async() => { h.setHighlightable({ dataKeys: ['Description'] }) // You have to specify to include the offset. // Currently offset is the only way to get a result, so if not set, you won't get a result. - h.setShape({ includeOffsets: true }) + h.setShape({ includeOffsets: true, textSnippets: { includeTextSnippets: true } }) }).build(); const result = await searcher.searchContents(request); expect(result?.results![0].highlight?.offsets?.data[0].value.length).toBeGreaterThan(0); + expect(result?.results![0].highlight?.snippets?.data[0].value[0]).toBe("...word should be highlighted"); }) \ No newline at end of file diff --git a/packages/client/tests/unit-tests/builders/search/contentSearchBuilder.unit.test.ts b/packages/client/tests/unit-tests/builders/search/contentSearchBuilder.unit.test.ts index f5103a3..4bfbc36 100644 --- a/packages/client/tests/unit-tests/builders/search/contentSearchBuilder.unit.test.ts +++ b/packages/client/tests/unit-tests/builders/search/contentSearchBuilder.unit.test.ts @@ -26,7 +26,10 @@ test('searchHightlighting', () => { maxSnippetsPerField: 3 }); h.setShape({ - includeOffsets: true + includeOffsets: true, + textSnippets: { + includeTextSnippets: true + } }); }).build(); @@ -38,4 +41,5 @@ test('searchHightlighting', () => { expect(subject.settings?.highlight?.limit.maxSnippetsPerEntry).toBe(2); expect(subject.settings?.highlight?.limit.maxSnippetsPerField).toBe(3); expect(subject.settings?.highlight?.shape.includeOffsets).toBe(true); + expect(subject.settings?.highlight?.shape.textSnippets?.includeTextSnippets).toBe(true); }); \ No newline at end of file From e9ced050210ed9c6026e09a7a332fec5a80aadb4 Mon Sep 17 00:00:00 2001 From: Simon Walenkamp Hansen Date: Thu, 9 Jan 2025 12:02:05 +0100 Subject: [PATCH 3/3] Self review --- .../client/src/builders/search/contentHighlightingBuilder.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/client/src/builders/search/contentHighlightingBuilder.ts b/packages/client/src/builders/search/contentHighlightingBuilder.ts index 1f161be..96ff356 100644 --- a/packages/client/src/builders/search/contentHighlightingBuilder.ts +++ b/packages/client/src/builders/search/contentHighlightingBuilder.ts @@ -39,7 +39,6 @@ export class ContentHighlightingBuilder { return this; } - public build(): ContentSearchSettingsHighlightSettings { return { $type: 'Relewise.Client.Requests.Search.Settings.ContentSearchSettings+HighlightSettings, Relewise.Client',