Skip to content

Commit 08ea1f9

Browse files
authored
Merge pull request #88 from Relewise/feat/search-highlighting-snippets
Feat: add support for search highlight shape snippet settings
2 parents 7f5e5ea + e9ced05 commit 08ea1f9

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

packages/client/src/builders/search/contentHighlightingBuilder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentHighlightProps, ContentSearchSettingsHighlightSettings, HighlightSettings2ContentContentHighlightPropsHighlightSettings2Limits, HighlightSettings2ContentContentHighlightPropsHighlightSettings2ResponseShape } from '../../models/data-contracts';
1+
import { ContentHighlightProps, ContentSearchSettingsHighlightSettings, HighlightSettings2ContentContentHighlightPropsHighlightSettings2Limits, HighlightSettings2ContentContentHighlightPropsHighlightSettings2ResponseShape, HighlightSettings2ContentContentHighlightPropsHighlightSettings2TextSnippetsSettings } from '../../models/data-contracts';
22

33
export class ContentHighlightingBuilder {
44
private enabledState: boolean = true;
@@ -32,8 +32,9 @@ export class ContentHighlightingBuilder {
3232
return this;
3333
}
3434

35-
public setShape(shape: { includeOffsets: boolean }): this {
35+
public setShape(shape: { includeOffsets: boolean, textSnippets?: HighlightSettings2ContentContentHighlightPropsHighlightSettings2TextSnippetsSettings }): this {
3636
this.shape.includeOffsets = shape.includeOffsets;
37+
this.shape.textSnippets = shape.textSnippets;
3738

3839
return this;
3940
}

packages/client/src/builders/search/productHighlightingBuilder.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HighlightSettings2ProductProductHighlightPropsHighlightSettings2Limits, HighlightSettings2ProductProductHighlightPropsHighlightSettings2ResponseShape, ProductHighlightProps, ProductSearchSettingsHighlightSettings } from '../../models/data-contracts';
1+
import { HighlightSettings2ProductProductHighlightPropsHighlightSettings2Limits, HighlightSettings2ProductProductHighlightPropsHighlightSettings2ResponseShape, HighlightSettings2ProductProductHighlightPropsHighlightSettings2TextSnippetsSettings, ProductHighlightProps, ProductSearchSettingsHighlightSettings } from '../../models/data-contracts';
22

33
export class ProductHighlightingBuilder {
44
private enabledState: boolean = true;
@@ -32,8 +32,9 @@ export class ProductHighlightingBuilder {
3232
return this;
3333
}
3434

35-
public setShape(shape: { includeOffsets: boolean }): this {
35+
public setShape(shape: { includeOffsets: boolean, textSnippets?: HighlightSettings2ProductProductHighlightPropsHighlightSettings2TextSnippetsSettings }): this {
3636
this.shape.includeOffsets = shape.includeOffsets;
37+
this.shape.textSnippets = shape.textSnippets;
3738

3839
return this;
3940
}

packages/client/tests/integration-tests/contentSearch.integration.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ test('Highlighting', async() => {
4444
h.setHighlightable({ dataKeys: ['Description'] })
4545
// You have to specify to include the offset.
4646
// Currently offset is the only way to get a result, so if not set, you won't get a result.
47-
h.setShape({ includeOffsets: true })
47+
h.setShape({ includeOffsets: true, textSnippets: { includeTextSnippets: true } })
4848
}).build();
4949

5050
const result = await searcher.searchContents(request);
5151

5252
expect(result?.results![0].highlight?.offsets?.data[0].value.length).toBeGreaterThan(0);
53+
expect(result?.results![0].highlight?.snippets?.data[0].value[0]).toBe("...word should be highlighted");
5354
})

packages/client/tests/integration-tests/productSearch.integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ test('Highlighting', async() => {
102102
h.setHighlightable({ dataKeys: ['Description'] })
103103
// You have to specify to include the offset.
104104
// Currently offset is the only way to get a result, so if not set, you won't get a result.
105-
h.setShape({ includeOffsets: true })
105+
h.setShape({ includeOffsets: true, textSnippets: { includeTextSnippets: true } })
106106
}).build();
107-
108107
const result = await searcher.searchProducts(request);
109108

110109
expect(result?.results![0].highlight?.offsets?.data[0].value.length).toBeGreaterThan(0);
110+
expect(result?.results![0].highlight?.snippets?.data[0].value[0]).toBe("...last word is highlighted");
111111
})

packages/client/tests/unit-tests/builders/search/contentSearchBuilder.unit.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ test('searchHightlighting', () => {
2626
maxSnippetsPerField: 3
2727
});
2828
h.setShape({
29-
includeOffsets: true
29+
includeOffsets: true,
30+
textSnippets: {
31+
includeTextSnippets: true
32+
}
3033
});
3134
}).build();
3235

@@ -38,4 +41,5 @@ test('searchHightlighting', () => {
3841
expect(subject.settings?.highlight?.limit.maxSnippetsPerEntry).toBe(2);
3942
expect(subject.settings?.highlight?.limit.maxSnippetsPerField).toBe(3);
4043
expect(subject.settings?.highlight?.shape.includeOffsets).toBe(true);
44+
expect(subject.settings?.highlight?.shape.textSnippets?.includeTextSnippets).toBe(true);
4145
});

packages/client/tests/unit-tests/builders/search/productSearchBuilder.unit.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ test('searchHightlighting', () => {
120120
maxSnippetsPerField: 3
121121
});
122122
h.setShape({
123-
includeOffsets: true
123+
includeOffsets: true,
124+
textSnippets: {
125+
includeTextSnippets: true,
126+
}
124127
});
125128
}).build();
126129

@@ -132,6 +135,7 @@ test('searchHightlighting', () => {
132135
expect(subject.settings?.highlight?.limit.maxSnippetsPerEntry).toBe(2);
133136
expect(subject.settings?.highlight?.limit.maxSnippetsPerField).toBe(3);
134137
expect(subject.settings?.highlight?.shape.includeOffsets).toBe(true);
138+
expect(subject.settings?.highlight?.shape.textSnippets?.includeTextSnippets).toBe(true);
135139
});
136140

137141
test('includeDisabled in ProductHasVariantsFilter', () => {

0 commit comments

Comments
 (0)