Skip to content

Commit

Permalink
feat: search_multi: add config OPTIMIZE_SEARCH
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Jan 21, 2025
1 parent e2ba38d commit 6497a22
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 23 additions & 0 deletions plugin/global/settings/settings.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ STYLE_COLOR = [
"#99CCFF", "#FF99CC", "#66CC66", "#CC99FF", "#FFCC66", "#FFFF80", "#DD9966", "#AAAAAA", "#66AAAA", "#DD6699",
]

# ------------------- 高级设置 -------------------
# 开启搜索优化
# true: 开启搜索优化
# false: 不开启搜索优化
# 说明(适用普通用户):
# 1. 假设搜索内容为 `h1:sour content:pear size>2kb`,其含义为:一级标题包含sour 并且 文件内容包含pear 并且 文件大小大于2KB
# 2. 程序默认是按输入的顺序进行逐级筛选的。即:先筛选出一级标题包含sour的文件,在前面基础上再筛选 content,在前面基础上再筛选 size
# 3. 然而,我们发现这三种查询属性所付出的代价是不一样的:
# - h1 需要读取并解析 md 文件
# - content 不需要解析文件,只需读取文件
# - size 不需要读取文件,只需读取文件属性
# 4. 为了提高搜索效率,一般要把低开销的逻辑移动到前面,让低开销的逻辑在最开始筛掉大多数不符合的文件,减少高开销逻辑的执行次数
# 5. 也就是说:最好要改成 `size>2kb content:pear h1:sour`
# 6. 本选项的功能就是:将上述的流程自动化,自动调整查询属性的位置,提高搜索效率
# 7. 此功能有一个小副作用:会影响高亮关键字的顺序
# 1. 高亮关键字的生成顺序本来是按照用户输入的顺序生成的。`h1:sour content:pear size>2kb` 的高亮关键字是:sour、pear
# 2. 结果现在用户输入被自动修改为 `size>2kb content:pear h1:sour`,导致高亮关键字是:pear、sour
# 3. 在页面上的体现就是:pear 的高亮色是蓝色,sour 是红色,现在两者发生了颜色对调
# 说明(适用程序员):
# 1. 优化 AST,将低开销的逻辑移动到前面,借助短路求值以提高查询效率
# 2. 优化 AST 需要对子树进行移动、旋转操作,导致评估生成的 tokens 顺序发生改变(Node reordering breaks token order)
OPTIMIZE_SEARCH = false


################### commander ###################
[commander]
Expand Down
9 changes: 5 additions & 4 deletions plugin/search_multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ class searchMultiPlugin extends BasePlugin {
this.utils.hide(this.entities.info)
}

getAST = (input = this.entities.input.value) => {
getAST = (input = this.entities.input.value, optimize = this.config.OPTIMIZE_SEARCH) => {
input = input.trim()
if (!input) return

try {
const ast = this.searcher.parse(input)
const ast = this.searcher.parse(input, optimize)
const explain = this.searcher.toExplain(ast)
this.entities.input.setAttribute("title", explain)
this.utils.notification.hide()
Expand Down Expand Up @@ -651,10 +651,11 @@ class Searcher {
]
}

parse(input) {
parse(input, optimize) {
input = this.config.CASE_SENSITIVE ? input : input.toLowerCase()
const ast = this.parser.parse(input)
return this.validateAndCast(ast)
this.validateAndCast(ast)
return optimize ? this.optimize(ast) : ast
}

validateAndCast(ast) {
Expand Down

0 comments on commit 6497a22

Please sign in to comment.