Skip to content

Commit ff3eee0

Browse files
committed
feat(docs): prioritize and dedupe meili results
* group hits based on item name * prioritize non-discord.js hits (since those should lead to the actual item implementation) resolves #236
1 parent ddb3d32 commit ff3eee0

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/functions/autocomplete/docsAutoComplete.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,39 @@ export async function djsMeiliSearch(query: string, version: string) {
169169
});
170170

171171
const docsResult = (await searchResult.json()) as any;
172-
const hits = docsResult.results.flatMap((res: any) => res.hits);
172+
173+
const groupedHits = new Map<string, [string, any][]>();
174+
175+
for (const result of docsResult.results) {
176+
const index = result.indexUid;
177+
for (const hit of result.hits) {
178+
const current = groupedHits.get(hit.name);
179+
if (!current) {
180+
groupedHits.set(hit.name, [[index, hit]]);
181+
continue;
182+
}
183+
184+
current.push([index, hit]);
185+
}
186+
}
187+
188+
const hits = [];
189+
190+
for (const group of groupedHits.values()) {
191+
const sorted = group.sort(([fstIndex], [sndIndex]) => {
192+
if (fstIndex.startsWith('discord-js')) {
193+
return 1;
194+
}
195+
196+
if (sndIndex.startsWith('discord.js')) {
197+
return -1;
198+
}
199+
200+
return 0;
201+
});
202+
203+
hits.push(sorted[0][1]);
204+
}
173205

174206
return {
175207
...docsResult,

0 commit comments

Comments
 (0)