Search: improve layer search code

This commit is contained in:
Pieter Vander Vennet 2024-09-24 17:23:44 +02:00
parent 2db7ef872c
commit d1fed39fcb
2 changed files with 30 additions and 13 deletions

View file

@ -9,16 +9,22 @@ export default class LayerSearch {
private readonly _layout: LayoutConfig private readonly _layout: LayoutConfig
private readonly _layerWhitelist: Set<string> private readonly _layerWhitelist: Set<string>
constructor(layout: LayoutConfig) { constructor(layout: LayoutConfig) {
this._layout = layout this._layout = layout
this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(<any>id) < 0)) this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(<any>id) < 0))
} }
static scoreLayers(query: string, layerWhitelist?: Set<string>): Record<string, number> { static scoreLayers(query: string, options: {
whitelist?: Set<string>, blacklist?: Set<string>
}): Record<string, number> {
const result: Record<string, number> = {} const result: Record<string, number> = {}
const queryParts = query.trim().split(" ").map(q => Utils.simplifyStringForSearch(q)) const queryParts = query.trim().split(" ").map(q => Utils.simplifyStringForSearch(q))
for (const id in ThemeSearch.officialThemes.layers) { for (const id in ThemeSearch.officialThemes.layers) {
if(layerWhitelist !== undefined && !layerWhitelist.has(id)){ if (options?.whitelist && !options?.whitelist.has(id)) {
continue
}
if (options?.blacklist?.has(id)) {
continue continue
} }
const keywords = ThemeSearch.officialThemes.layers[id] const keywords = ThemeSearch.officialThemes.layers[id]
@ -38,7 +44,7 @@ export default class LayerSearch {
for (const layer in scores) { for (const layer in scores) {
asList.push({ asList.push({
layer: this._layout.getLayer(layer), layer: this._layout.getLayer(layer),
score: scores[layer] score: scores[layer],
}) })
} }
asList.sort((a, b) => a.score - b.score) asList.sort((a, b) => a.score - b.score)

View file

@ -13,7 +13,7 @@ type ThemeSearchScore = {
theme: MinimalLayoutInformation, theme: MinimalLayoutInformation,
lowest: number, lowest: number,
perLayer?: Record<string, number>, perLayer?: Record<string, number>,
other: number other: number,
} }
@ -92,15 +92,26 @@ export default class ThemeSearch {
return `${linkPrefix}` return `${linkPrefix}`
} }
private static scoreThemes(query: string, themes: MinimalLayoutInformation[], ignoreLayers: string[] = []): Record<string, ThemeSearchScore> { /**
* Returns a score based on textual search
*
* Note that, if `query.length < 3`, layers are _not_ searched because this takes too much time
* @param query
* @param themes
* @param ignoreLayers
* @private
*/
private static scoreThemes(query: string, themes: MinimalLayoutInformation[], ignoreLayers: string[] = undefined): Record<string, ThemeSearchScore> {
if (query?.length < 1) { if (query?.length < 1) {
return undefined return undefined
} }
themes = Utils.NoNullInplace(themes) themes = Utils.NoNullInplace(themes)
const layerScores = LayerSearch.scoreLayers(query)
for (const ignoreLayer of ignoreLayers) { let options : {blacklist: Set<string>} = undefined
delete layerScores[ignoreLayer] if(ignoreLayers?.length > 0){
options.blacklist = new Set(ignoreLayers)
} }
const layerScores = query.length < 3 ? {} : LayerSearch.scoreLayers(query, options)
const results: Record<string, ThemeSearchScore> = {} const results: Record<string, ThemeSearchScore> = {}
for (const layoutInfo of themes) { for (const layoutInfo of themes) {
const theme = layoutInfo.id const theme = layoutInfo.id
@ -111,7 +122,7 @@ export default class ThemeSearch {
results[theme] = { results[theme] = {
theme: layoutInfo, theme: layoutInfo,
lowest: -1, lowest: -1,
other: 0, other: 0
} }
continue continue
} }