Skip to content

Commit c3ea3e4

Browse files
chore: wip
1 parent 318d736 commit c3ea3e4

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

.stacks/core/search-engine/src/drivers/meilisearch.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,44 @@ function client(options?: MeiliSearchOptions) {
2525
}
2626

2727
async function search(index: string, params: any): Promise<SearchResponse<Record<string, any>>> {
28+
const offsetVal = ((params.page * params.perPage) - 20) || 0
29+
const filter = convertToMeilisearchFilter(params.filter)
30+
const sort = convertToMeilisearchSorting(params.sort)
31+
2832
return await client()
2933
.index(index)
30-
.search(params.query, { limit: params.perPage, offset: params.page * params.perPage })
34+
.search(params.query, { limit: params.perPage, filter, sort, offset: offsetVal })
35+
}
36+
37+
function convertToMeilisearchFilter(jsonData: any): string[] {
38+
const filters: string[] = [];
39+
40+
for (const key in jsonData) {
41+
if (jsonData.hasOwnProperty(key)) {
42+
const value = jsonData[key];
43+
const filter = `${key}='${value}'`;
44+
filters.push(filter);
45+
}
46+
}
47+
48+
return filters;
3149
}
3250

51+
function convertToMeilisearchSorting(jsonData: any): string[] {
52+
const filters: string[] = [];
53+
54+
for (const key in jsonData) {
55+
if (jsonData.hasOwnProperty(key)) {
56+
const value = jsonData[key];
57+
const filter = `${key}:${value}`;
58+
filters.push(filter);
59+
}
60+
}
61+
62+
return filters;
63+
}
64+
65+
3366
export default {
3467
client,
3568
search,

0 commit comments

Comments
 (0)