diff --git a/src/Logic/Search/LayerSearch.ts b/src/Logic/Search/LayerSearch.ts index b4a17a87d..02a58d5b8 100644 --- a/src/Logic/Search/LayerSearch.ts +++ b/src/Logic/Search/LayerSearch.ts @@ -8,21 +8,27 @@ import { Utils } from "../../Utils" export default class LayerSearch { private readonly _layout: LayoutConfig - private readonly _layerWhitelist : Set + private readonly _layerWhitelist: Set + constructor(layout: LayoutConfig) { this._layout = layout - this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf( id) < 0)) + this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(id) < 0)) } - static scoreLayers(query: string, layerWhitelist?: Set): Record { + static scoreLayers(query: string, options: { + whitelist?: Set, blacklist?: Set + }): Record { const result: Record = {} const queryParts = query.trim().split(" ").map(q => Utils.simplifyStringForSearch(q)) 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 } const keywords = ThemeSearch.officialThemes.layers[id] - const distance = Math.min(... queryParts.map(q => SearchUtils.scoreKeywords(q, keywords))) + const distance = Math.min(...queryParts.map(q => SearchUtils.scoreKeywords(q, keywords))) result[id] = distance } return result @@ -34,11 +40,11 @@ export default class LayerSearch { return [] } const scores = LayerSearch.scoreLayers(query, this._layerWhitelist) - const asList:({layer: LayerConfig, score:number})[] = [] + const asList: ({ layer: LayerConfig, score: number })[] = [] for (const layer in scores) { asList.push({ layer: this._layout.getLayer(layer), - score: scores[layer] + score: scores[layer], }) } asList.sort((a, b) => a.score - b.score) diff --git a/src/Logic/Search/ThemeSearch.ts b/src/Logic/Search/ThemeSearch.ts index 07d2d8bb0..218a7ad63 100644 --- a/src/Logic/Search/ThemeSearch.ts +++ b/src/Logic/Search/ThemeSearch.ts @@ -13,7 +13,7 @@ type ThemeSearchScore = { theme: MinimalLayoutInformation, lowest: number, perLayer?: Record, - other: number + other: number, } @@ -92,15 +92,26 @@ export default class ThemeSearch { return `${linkPrefix}` } - private static scoreThemes(query: string, themes: MinimalLayoutInformation[], ignoreLayers: string[] = []): Record { + /** + * 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 { if (query?.length < 1) { return undefined } themes = Utils.NoNullInplace(themes) - const layerScores = LayerSearch.scoreLayers(query) - for (const ignoreLayer of ignoreLayers) { - delete layerScores[ignoreLayer] + + let options : {blacklist: Set} = undefined + if(ignoreLayers?.length > 0){ + options.blacklist = new Set(ignoreLayers) } + const layerScores = query.length < 3 ? {} : LayerSearch.scoreLayers(query, options) const results: Record = {} for (const layoutInfo of themes) { const theme = layoutInfo.id @@ -111,7 +122,7 @@ export default class ThemeSearch { results[theme] = { theme: layoutInfo, lowest: -1, - other: 0, + other: 0 } continue }