Search: improve layer search code
This commit is contained in:
parent
2db7ef872c
commit
d1fed39fcb
2 changed files with 30 additions and 13 deletions
|
@ -8,21 +8,27 @@ import { Utils } from "../../Utils"
|
|||
export default class LayerSearch {
|
||||
|
||||
private readonly _layout: LayoutConfig
|
||||
private readonly _layerWhitelist : Set<string>
|
||||
private readonly _layerWhitelist: Set<string>
|
||||
|
||||
constructor(layout: LayoutConfig) {
|
||||
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 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)
|
||||
|
|
|
@ -13,7 +13,7 @@ type ThemeSearchScore = {
|
|||
theme: MinimalLayoutInformation,
|
||||
lowest: number,
|
||||
perLayer?: Record<string, number>,
|
||||
other: number
|
||||
other: number,
|
||||
}
|
||||
|
||||
|
||||
|
@ -92,15 +92,26 @@ export default class ThemeSearch {
|
|||
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) {
|
||||
return undefined
|
||||
}
|
||||
themes = Utils.NoNullInplace(themes)
|
||||
const layerScores = LayerSearch.scoreLayers(query)
|
||||
for (const ignoreLayer of ignoreLayers) {
|
||||
delete layerScores[ignoreLayer]
|
||||
|
||||
let options : {blacklist: Set<string>} = undefined
|
||||
if(ignoreLayers?.length > 0){
|
||||
options.blacklist = new Set(ignoreLayers)
|
||||
}
|
||||
const layerScores = query.length < 3 ? {} : LayerSearch.scoreLayers(query, options)
|
||||
const results: Record<string, ThemeSearchScore> = {}
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue