Skip to content

Commit

Permalink
Merge pull request #88 from Relewise/feat/search-highlighting-snippets
Browse files Browse the repository at this point in the history
Feat: add support for search highlight shape snippet settings
  • Loading branch information
mzanoni authored Jan 9, 2025
2 parents 7f5e5ea + e9ced05 commit 08ea1f9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,8 +32,9 @@ 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ test('searchHightlighting', () => {
maxSnippetsPerField: 3
});
h.setShape({
includeOffsets: true
includeOffsets: true,
textSnippets: {
includeTextSnippets: true
}
});
}).build();

Expand All @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ test('searchHightlighting', () => {
maxSnippetsPerField: 3
});
h.setShape({
includeOffsets: true
includeOffsets: true,
textSnippets: {
includeTextSnippets: true,
}
});
}).build();

Expand All @@ -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', () => {
Expand Down

0 comments on commit 08ea1f9

Please sign in to comment.