You need to enable JavaScript to run this app. 使用search-index检索JS对象数组时如何清理LevelDB历史数据或找替代方案
优惠活动
大模型
产品
解决方案
定价
更多

使用search-index检索JS对象数组时如何清理LevelDB历史数据或找替代方案

问题解决方案

现有search-index工具的历史数据清理方案

  • 启用内存运行模式:初始化search-index时指定indexPath: ':memory:',所有索引数据只会存储在运行内存中,不会写入本地磁盘的LevelDB数据库,实例销毁后数据自动清空,完全不会留存历史数据,是优先选择的方案。初始化示例代码:
const si = require('search-index')
const searchInst = await si({
  indexPath: ':memory:'
})
  • 磁盘模式下手动清库:如果需要保留磁盘存储的用法,每次检索完成后可以调用DELETE接口批量清空所有已索引的文档,示例代码:
await searchInst.DELETE({
  query: [{
    FIELD: '*',
    VALUE: '*'
  }]
})

替代工具推荐

你需要支持嵌套对象多路径全文检索、无需持久化历史数据的场景,以下工具都可以满足需求:

1. Lunr.js

轻量级全文检索库,纯内存运行无本地存储,支持自定义嵌套字段作为检索范围,适配你不同对象的不同文本存储路径需求。

const lunr = require('lunr')
// 初始化索引,注册所有需要检索的字段路径
const idx = lunr(function () {
  this.field('dmodule.identAndStatusSection')
  this.field('dmodule.content')
  this.field('dml.identAndStatusSection')
  this.field('dml.dmlContent')
  this.field('pm.identAndStatusSection')
  this.field('pm.content')
  // 批量添加文档
  targetArray.forEach((doc, index) => {
    this.add({ id: index, ...doc })
  })
})
// 执行搜索
const res = idx.search('搜索关键词')

2. MiniSearch

比Lunr更轻量化,支持通配符匹配嵌套字段路径,不需要逐条列举所有路径,更适配你结构不一致的对象数组检索场景,同样纯内存运行。

const MiniSearch = require('minisearch')
const searchInst = new MiniSearch({
  // 用通配符匹配所有一级key下的对应字段,无需单独列dmodule、dml、pm的路径
  fields: ['*.identAndStatusSection', '*.content', '*.dmlContent']
})
// 批量添加文档
searchInst.addAll(targetArray.map((item, idx) => ({ id: idx, ...item })))
// 执行搜索
const res = searchInst.search('搜索关键词')

3. Fuse.js

无分词依赖的模糊检索库,体积最小,配置简单,直接指定需要检索的嵌套路径即可使用,适合不需要复杂分词能力的场景。

const Fuse = require('fuse.js')
const options = {
  keys: ['dmodule.identAndStatusSection', 'dmodule.content', 'dml.identAndStatusSection', 'dml.dmlContent', 'pm.identAndStatusSection', 'pm.content']
}
const fuse = new Fuse(targetArray, options)
// 执行搜索
const res = fuse.search('搜索关键词')

内容的提问来源于stack exchange,提问作者user16192387

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 18:06:07