Skip to content

Commit

Permalink
fix(algolia): improve resilience to unexpected data shape
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
almostSouji committed Oct 22, 2024
1 parent c8f5927 commit 379e686
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/functions/autocomplete/algoliaAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ function headingIsSimilar(one: string, other: string) {
export function resolveHitToNamestring(hit: AlgoliaHit) {
const { hierarchy } = hit;

const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy).map((heading) => removeDtypesPrefix(heading));
const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy)
.filter(Boolean)
.map((heading) => removeDtypesPrefix(heading));

const headingParts = [];

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

const mostSpecific = restLevels.filter(Boolean).at(-1);
if (mostSpecific?.length && !headingIsSimilar(lvl0, mostSpecific) && !headingIsSimilar(lvl1, mostSpecific)) {
const mostSpecific = restLevels.at(-1);
if (mostSpecific?.length && mostSpecific !== lvl0 && mostSpecific !== lvl1) {
headingParts.push(`- ${mostSpecific}`);
}

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

function autoCompleteMap(elements: AlgoliaHit[]) {
const uniqueElements = elements.filter(dedupeAlgoliaHits());
return uniqueElements.map((element) => ({
name: truncate(resolveHitToNamestring(element), 90, ''),
value: compactAlgoliaObjectId(element.objectID),
}));
return uniqueElements
.filter((element) => {
const value = compactAlgoliaObjectId(element.objectID);
// API restriction. Cannot resolve from truncated, so filtering here.
return value.length <= 100;
})
.map((element) => ({
name: truncate(resolveHitToNamestring(element), 100, ''),
value: compactAlgoliaObjectId(element.objectID),
}));
}

export async function algoliaAutoComplete(
Expand Down

0 comments on commit 379e686

Please sign in to comment.