Raneto中文搜索支持

背景

因业务部门需要在线软件使用说明文档,但我们资源不足,故我想找一个开源的知识库,发现 Raneto不错,决定使用。
官方文档相当清晰,部署完成,发布一些文章,启动项目,交由业务同事测试使用,于是我收到 中文搜索 不支持反馈。查看其配置文件

example/config.default.js 
// Support search with extra languages
searchExtraLanguages: ['ru'],在 `Raneto/node_modules/lunr-languages/`列表下并没有 `lunr.zh.js` 中文搜索支持文件,故此路不通。那就谷歌解决此问题了。

环境

Centos
Raneto 0.16.2

需求

二次开发 Raneto 中文搜索支持

解决

  • 重命名 lunr 文件夹

cd Raneto
mv  ./node_modules/lunr  node_modules/lunr_official
  • 下载 https://github.com/codepiano/lunr.js 文件夹并重命名为lunr

cd  Raneto/node_modules
git clone https://github.com/codepiano/lunr.js && mv lunr.js lunr
  • 修改 Raneto/app/core/search.js 文件

1.复制该文件到 Raneto/app/core/目录里

cp  Raneto/node_modules/lunr/lunr.js  Raneto/app/core/

2.直接引用其 https://github.com/codepiano/lunr.js 的 lunr.js 文件,也就是Raneto/app/core/lunr.js,另外,注释其语言加载配置。

function getLunr (config) {
  if (instance === null) {
    // instance = require('lunr');
    instance = require('./lunr.js');
    // require('lunr-languages/lunr.stemmer.support')(instance);
    // require('lunr-languages/lunr.multi')(instance);
    // config.searchExtraLanguages.forEach(lang =>
    // require('lunr-languages/lunr.' + lang)(instance)
    // );
  }
  return instance;

3.注释默认设置加载的lunr配置

function handler (query, config) {
  const contentDir = utils.normalizeDir(path.normalize(config.content_dir));
  const documents = glob
    .sync(contentDir + '**/*.md')
    .map(filePath => contentProcessors.extractDocument(
      contentDir, filePath, config.debug
    ))
    .filter(doc => doc !== null);

  const lunrInstance = getLunr(config);
  const idx = lunrInstance(function () {
    // 注释默认设置加载的lunr配置
    // this.use(getStemmers(config));
    this.field('title');
    this.field('body');
    this.ref('id');
    documents.forEach((doc) => this.add(doc), this);
  });
  • 安装 nodejieba模块

npm install --save nodejieba

最后再次启动,支持中文搜索了

cd Raneto
npm start

引用

Raneto配置中文搜索|无声又无息

posted @ 2019-03-06 15:11  zhoucx6  阅读(1364)  评论(0编辑  收藏  举报