2022-10-26 18:01:40 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022, KylinSoft Co., Ltd.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* Authors: iaom <zhangpengfei@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "basic-indexer.h"
|
2023-04-19 10:31:51 +08:00
|
|
|
|
#include <QDateTime>
|
2022-10-26 18:01:40 +08:00
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QUrl>
|
2023-07-24 11:39:18 +08:00
|
|
|
|
#include <QTextBoundaryFinder>
|
2023-04-19 10:31:51 +08:00
|
|
|
|
#include "file-utils.h"
|
2022-10-26 18:01:40 +08:00
|
|
|
|
using namespace UkuiSearch;
|
|
|
|
|
BasicIndexer::BasicIndexer(const QString& filePath): m_filePath(filePath)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BasicIndexer::index()
|
|
|
|
|
{
|
|
|
|
|
QFileInfo info = QFileInfo(m_filePath);
|
2022-12-23 11:11:06 +08:00
|
|
|
|
if(!info.exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-10-26 18:01:40 +08:00
|
|
|
|
//添加数据
|
|
|
|
|
m_document.setData(m_filePath);
|
|
|
|
|
//唯一term
|
|
|
|
|
m_document.setUniqueTerm(FileUtils::makeDocUterm(m_filePath));
|
|
|
|
|
//上层文件夹term,用于在上层文件夹删除时删除自己
|
2022-12-05 15:58:33 +08:00
|
|
|
|
m_document.addTerm("PARENTTERM" + FileUtils::makeDocUterm(m_filePath.section("/", 0, -2, QString::SectionIncludeLeadingSep)));
|
2022-10-26 18:01:40 +08:00
|
|
|
|
//1-目录, 0-文件
|
|
|
|
|
m_document.addValue(1, QString((info.isDir() && (!info.isSymLink())) ? "1" : "0"));
|
|
|
|
|
//修改时间
|
|
|
|
|
QString time = info.lastModified().toString("yyyyMMddHHmmsszzz");
|
|
|
|
|
m_document.setIndexTime(time);
|
|
|
|
|
m_document.addSortableSerialiseValue(2, time);
|
|
|
|
|
|
|
|
|
|
QString indexName = info.fileName().toLower();
|
|
|
|
|
QStringList pinyinTextList = FileUtils::findMultiToneWords(info.fileName());
|
|
|
|
|
int i = 0;
|
|
|
|
|
int postingCount = 1; //terms post of Xapian document is started from 1!
|
2023-07-24 11:39:18 +08:00
|
|
|
|
int start = 0;
|
|
|
|
|
QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, indexName);
|
|
|
|
|
for(; bf.position() != -1; bf.toNextBoundary()) {
|
|
|
|
|
int end = bf.position();
|
|
|
|
|
if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) {
|
|
|
|
|
m_document.addPosting(QUrl::toPercentEncoding(indexName.mid(start, end - start)).toStdString(), postingCount);
|
|
|
|
|
++postingCount;
|
|
|
|
|
}
|
|
|
|
|
start = end;
|
2022-10-26 18:01:40 +08:00
|
|
|
|
}
|
2023-07-24 11:39:18 +08:00
|
|
|
|
|
2022-10-26 18:01:40 +08:00
|
|
|
|
for(QString& s : pinyinTextList) {
|
|
|
|
|
i = 0;
|
|
|
|
|
while(i < s.size()) {
|
|
|
|
|
m_document.addPosting(QUrl::toPercentEncoding(s.at(i)).toStdString(), postingCount);
|
|
|
|
|
++postingCount;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|