Skip to content

Commit 379e686

Browse files
committed
fix(algolia): improve resilience to unexpected data shape
* filter compact id > 100 * be less harsh with most specific hierarchy dedupes * filter hierarchy earlier to handle lvl0/lvl1 null values * allow 100 length in name string to match api restriction
1 parent c8f5927 commit 379e686

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/functions/autocomplete/algoliaAutoComplete.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function headingIsSimilar(one: string, other: string) {
2828
export function resolveHitToNamestring(hit: AlgoliaHit) {
2929
const { hierarchy } = hit;
3030

31-
const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy).map((heading) => removeDtypesPrefix(heading));
31+
const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy)
32+
.filter(Boolean)
33+
.map((heading) => removeDtypesPrefix(heading));
3234

3335
const headingParts = [];
3436

@@ -38,8 +40,8 @@ export function resolveHitToNamestring(hit: AlgoliaHit) {
3840
headingParts.push(`${lvl0}:`, lvl1);
3941
}
4042

41-
const mostSpecific = restLevels.filter(Boolean).at(-1);
42-
if (mostSpecific?.length && !headingIsSimilar(lvl0, mostSpecific) && !headingIsSimilar(lvl1, mostSpecific)) {
43+
const mostSpecific = restLevels.at(-1);
44+
if (mostSpecific?.length && mostSpecific !== lvl0 && mostSpecific !== lvl1) {
4345
headingParts.push(`- ${mostSpecific}`);
4446
}
4547

@@ -48,10 +50,16 @@ export function resolveHitToNamestring(hit: AlgoliaHit) {
4850

4951
function autoCompleteMap(elements: AlgoliaHit[]) {
5052
const uniqueElements = elements.filter(dedupeAlgoliaHits());
51-
return uniqueElements.map((element) => ({
52-
name: truncate(resolveHitToNamestring(element), 90, ''),
53-
value: compactAlgoliaObjectId(element.objectID),
54-
}));
53+
return uniqueElements
54+
.filter((element) => {
55+
const value = compactAlgoliaObjectId(element.objectID);
56+
// API restriction. Cannot resolve from truncated, so filtering here.
57+
return value.length <= 100;
58+
})
59+
.map((element) => ({
60+
name: truncate(resolveHitToNamestring(element), 100, ''),
61+
value: compactAlgoliaObjectId(element.objectID),
62+
}));
5563
}
5664

5765
export async function algoliaAutoComplete(

0 commit comments

Comments
 (0)