Skip to content

Commit

Permalink
feat: Enforce search limit
Browse files Browse the repository at this point in the history
Use flexsearch API rather than custom method
  • Loading branch information
paultranvan committed Oct 21, 2024
1 parent 4b9b180 commit a9cebf0
Showing 1 changed file with 3 additions and 30 deletions.
33 changes: 3 additions & 30 deletions src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class SearchEngine {

const allResults = this.searchOnIndexes(query)
const results = this.deduplicateAndFlatten(allResults)
const sortedResults = this.sortAndLimitSearchResults(results)
const sortedResults = this.sortSearchResults(results)

return sortedResults
.map(res => normalizeSearchResult(this.client, res, query))
Expand All @@ -256,7 +256,8 @@ class SearchEngine {
}
// TODO: do not use flexsearch store and rely on pouch storage?
// It's better for memory, but might slow down search queries
const indexResults = index.index.search(query, LIMIT_DOCTYPE_SEARCH, {
const indexResults = index.index.search(query, {
limit: LIMIT_DOCTYPE_SEARCH,
enrich: true
})
const newResults = indexResults.map(res => ({
Expand Down Expand Up @@ -288,13 +289,6 @@ class SearchEngine {
return [...resultMap.values()]
}

sortAndLimitSearchResults(
searchResults: RawSearchResult[]
): RawSearchResult[] {
const sortedResults = this.sortSearchResults(searchResults)
return this.limitSearchResults(sortedResults)
}

compareStrings(str1: string, str2: string): number {
return str1.localeCompare(str2, undefined, { numeric: true })
}
Expand Down Expand Up @@ -340,27 +334,6 @@ class SearchEngine {
}
return this.compareStrings(aRes.doc.name, bRes.doc.name)
}

limitSearchResults(searchResults: RawSearchResult[]): RawSearchResult[] {
const limitedResults = {
[APPS_DOCTYPE]: [],
[CONTACTS_DOCTYPE]: [],
[FILES_DOCTYPE]: []
}

searchResults.forEach(item => {
const type = item.doctype as SearchedDoctype
if (limitedResults[type].length < LIMIT_DOCTYPE_SEARCH) {
limitedResults[type].push(item)
}
})

return [
...limitedResults[APPS_DOCTYPE],
...limitedResults[CONTACTS_DOCTYPE],
...limitedResults[FILES_DOCTYPE]
]
}
}

export default SearchEngine

0 comments on commit a9cebf0

Please sign in to comment.