Skip to content

Commit

Permalink
Add ability to turn off group flattening.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbosco committed Jul 26, 2023
1 parent 07e82aa commit 04bcb34
Show file tree
Hide file tree
Showing 13 changed files with 940 additions and 13 deletions.
19 changes: 16 additions & 3 deletions dist/typesense-instantsearch-adapter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense-instantsearch-adapter.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ interface BaseAdapterOptions {
geoLocationField?: string;
facetableFieldsWithSpecialCharacters?: string[];
renderingContent?: object;
flattenGroupedHits?: boolean;
}

interface CollectionSearchParameters {
Expand Down
4 changes: 3 additions & 1 deletion lib/Configuration.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Configuration.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions lib/SearchResponseAdapter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/SearchResponseAdapter.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class Configuration {
});

this.renderingContent = options.renderingContent;
this.flattenGroupedHits = options.flattenGroupedHits ?? true;
}

validate() {
Expand Down
16 changes: 14 additions & 2 deletions src/SearchResponseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ export class SearchResponseAdapter {

// adaptedResult is now in the form of [[{}, {}], [{}, {}], ...]
// where each element in the outermost array corresponds to a group.
// We now flatten it to [{}, {}, {}]
adaptedResult = adaptedResult.flat();

if (this.configuration.flattenGroupedHits) {
// We flatten it to [{}, {}, {}]
adaptedResult = adaptedResult.flat();
} else {
// We flatten it to [{ ..., grouped_hits: [{}, {}] }, {}, {}]
// We set the first element in the group as the hit, and then add a new key called grouped_hits with the other hits
adaptedResult = adaptedResult.map((adaptedGroupedHit) => {
return {
...adaptedGroupedHit[0],
_grouped_hits: adaptedGroupedHit,
};
});
}

return adaptedResult;
}
Expand Down
Loading

0 comments on commit 04bcb34

Please sign in to comment.