Skip to content

Commit

Permalink
feat(api): V4-1136: filter empty categories in search
Browse files Browse the repository at this point in the history
  • Loading branch information
mechkg committed May 17, 2024
1 parent c256fc8 commit af4fe99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
29 changes: 22 additions & 7 deletions apps/api/src/food-index/workers/food-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { CategoryHeader } from '@intake24/common/types/http';
import type { AlternativeFoodNames } from '@intake24/db';
import { Category, CategoryLocal, FoodLocalList, RecipeFoods, SynonymSet } from '@intake24/db';

Expand All @@ -11,6 +10,12 @@ export type LocalFoodData = {
parentCategories: Set<string>;
};

export type LocalCategoryData = {
code: string;
name: string;
parentCategories: Set<string>;
};

export type GlobalCategoryEntry = {
isHidden: boolean;
parentCategories: Set<string>;
Expand Down Expand Up @@ -76,17 +81,27 @@ export async function fetchLocalFoods(localeId: string): Promise<LocalFoodData[]
});
}

export async function fetchLocalCategories(localeId: string): Promise<CategoryHeader[]> {
export async function fetchLocalCategories(localeId: string): Promise<LocalCategoryData[]> {
const localCategories = await CategoryLocal.findAll({
where: { localeId },
attributes: ['categoryCode', 'name'],
include: { required: true, association: 'main', attributes: [], where: { isHidden: false } },
include: { required: true, association: 'main', attributes: ['code'], include: [
{
association: 'parentCategories',
attributes: ['code'],
},
], where: { isHidden: false } },
});

return localCategories.map(row => ({
code: row.categoryCode,
name: row.name,
}));
return localCategories.map((row) => {
const parentCategories = new Set(row.main!.parentCategories!.map(row => row.code));

return ({
code: row.categoryCode,
name: row.name,
parentCategories,
});
});
}

/**
Expand Down
24 changes: 23 additions & 1 deletion apps/api/src/food-index/workers/index-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ databases.init();

interface LocalFoodIndex {
foodParentCategories: Map<string, Set<string>>;
nonEmptyCategories: Set<string>;
foodIndex: PhraseIndex<string>;
categoryIndex: PhraseIndex<string>;
}
Expand Down Expand Up @@ -160,8 +161,28 @@ async function buildIndexForLocale(localeId: string): Promise<LocalFoodIndex> {
localFoods.map(food => [food.code, food.parentCategories]),
);

const categoryParentCategories = new Map(
localCategories.map(category => [category.code, category.parentCategories]),
);

const nonEmptyCategories = new Set<string>();

function markNonEmpty(categoryCode: string): void {
nonEmptyCategories.add(categoryCode);

const parentCategories = categoryParentCategories.get(categoryCode);

if (parentCategories !== undefined) {
for (const parentCategoryCode of parentCategories)
markNonEmpty(parentCategoryCode);
}
}

localFoods.forEach(food => food.parentCategories.forEach(categoryCode => markNonEmpty(categoryCode)));

return {
foodParentCategories,
nonEmptyCategories,
foodIndex,
categoryIndex,
};
Expand Down Expand Up @@ -352,7 +373,8 @@ async function queryIndex(query: SearchQuery): Promise<FoodSearchResponse> {

const filteredCategories = categoryResults.filter(
matchResult =>
query.parameters.limitToCategory === undefined || isSubcategory(matchResult.key, query.parameters.limitToCategory),
(query.parameters.limitToCategory === undefined || isSubcategory(matchResult.key, query.parameters.limitToCategory))
&& localeIndex.nonEmptyCategories.has(matchResult.key),
);

const categories = rankCategoryResults(filteredCategories);
Expand Down

0 comments on commit af4fe99

Please sign in to comment.