MediaWiki如何索引外部挂载目录文件并实现全文搜索?
索引外部挂载目录文档并实现MediaWiki全文搜索方案
环境与需求概述
当前环境:Ubuntu 20.04 + MediaWiki 1.39.3 + PostgreSQL 12.15 + Elasticsearch 7.10.2 + CirrusSearch 6.5.4 + Elastica 6.2.0 + TikaAllTheFiles 1.0.1
需求:在现有可搜索上传PDF的基础上,实现对外部挂载目录中PDF、DOCX、PPTX文件的全文索引与搜索
操作步骤
1. 确保外部挂载目录的权限与持久化
- 给MediaWiki运行用户(默认
www-data)赋予目录读取权限:chown -R www-data:www-data /path/to/your/external/mount chmod -R 755 /path/to/your/external/mount - 编辑
/etc/fstab添加挂载条目,确保服务器重启后目录自动挂载(以NFS为例):
执行server:/remote/path /local/mount/path nfs defaults 0 0mount -a验证挂载生效。
2. 编写文档扫描与文本提取脚本
复用TikaAllTheFiles的Tika配置,编写PHP维护脚本(放在MediaWiki根目录的maintenance文件夹下,命名为IndexExternalDocs.php),实现目录遍历、文本提取与索引同步:
<?php require_once __DIR__ . '/Maintenance.php'; class IndexExternalDocs extends Maintenance { public function __construct() { parent::__construct(); $this->addOption('dir', 'External directory to index', true, true); $this->requireExtension('CirrusSearch'); $this->requireExtension('Elastica'); } public function execute() { $dir = $this->getOption('dir'); $tikaPath = $GLOBALS['wgTikaAllTheFilesJarPath']; $client = CirrusSearch\Connection::getClient(); $indexName = $GLOBALS['wgCirrusSearchExtraIndexes']['external_docs'] ?? 'mediawiki_external_docs'; $index = $client->getIndex($indexName); // 首次运行创建索引映射 if (!$index->exists()) { $mapping = [ 'mappings' => [ 'properties' => [ 'filename' => ['type' => 'text'], 'path' => ['type' => 'keyword'], 'content' => ['type' => 'text'], 'file_type' => ['type' => 'keyword'], 'modified' => ['type' => 'date'] ] ] ]; $index->create($mapping); } // 遍历目标文件 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); foreach ($iterator as $file) { if (!$file->isFile()) continue; $ext = strtolower($file->getExtension()); if (!in_array($ext, ['pdf', 'docx', 'pptx'])) continue; $filePath = $file->getPathname(); $modified = $file->getMTime() * 1000; // 跳过未修改的已索引文件 $doc = $index->getDocumentById(md5($filePath)); if ($doc && $doc->get('modified') == $modified) continue; // 调用Tika提取文本内容 $text = shell_exec("java -jar {$tikaPath} -t " . escapeshellarg($filePath)); if (!$text) continue; // 构建并推送索引文档 $document = new \Elastica\Document(md5($filePath), [ 'filename' => $file->getFilename(), 'path' => $filePath, 'content' => $text, 'file_type' => $ext, 'modified' => $modified ]); $index->addDocument($document); $this->output("Indexed: {$filePath}\n"); } $index->refresh(); $this->output("Indexing completed.\n"); } } $maintClass = IndexExternalDocs::class; require_once RUN_MAINTENANCE_IF_MAIN;
3. 配置CirrusSearch支持外部文档索引
在MediaWiki的LocalSettings.php中添加以下配置,让搜索系统识别外部索引:
// 注册外部文档索引 $wgCirrusSearchExtraIndexes['external_docs'] = 'mediawiki_external_docs'; // 将外部索引加入搜索范围 $wgCirrusSearchSearchIndexes = array_merge($wgCirrusSearchSearchIndexes, ['external_docs']); // 配置搜索结果显示字段 $wgCirrusSearchResultProfiles['external_docs'] = [ 'fields' => ['filename', 'path', 'modified'], 'snippet' => ['field' => 'content'] ];
4. 首次运行索引脚本
切换到MediaWiki运行用户执行初始化索引:
su - www-data -s /bin/bash cd /var/www/mediawiki php maintenance/IndexExternalDocs.php --dir=/path/to/your/external/mount
5. 设置定期同步任务
添加cron任务,每天凌晨自动扫描更新外部目录的文件:
crontab -e -u www-data
添加以下条目:
0 2 * * * cd /var/www/mediawiki && php maintenance/IndexExternalDocs.php --dir=/path/to/your/external/mount >> /var/log/mediawiki/external_docs_index.log 2>&1
6. 验证搜索功能
在MediaWiki搜索框输入关键词,检查是否能返回外部目录中的文档结果,点击结果可查看文件名、路径及内容片段。
内容的提问来源于stack exchange,提问作者copossum
相关产品推荐
相关产品推荐

