2021-03-17 10:23:21 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020, 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: zhangpengfei <zhangpengfei@kylinos.cn>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "search-manager.h"
|
2023-09-28 10:54:58 +08:00
|
|
|
|
|
|
|
|
|
#include <utility>
|
2022-04-08 11:15:18 +08:00
|
|
|
|
#include "dir-watcher.h"
|
2023-12-15 13:43:42 +08:00
|
|
|
|
#include "file-indexer-config.h"
|
|
|
|
|
|
2021-12-14 14:43:35 +08:00
|
|
|
|
using namespace UkuiSearch;
|
2022-01-21 16:53:19 +08:00
|
|
|
|
|
2021-08-24 17:14:28 +08:00
|
|
|
|
size_t SearchManager::uniqueSymbolFile = 0;
|
|
|
|
|
size_t SearchManager::uniqueSymbolDir = 0;
|
|
|
|
|
size_t SearchManager::uniqueSymbolContent = 0;
|
|
|
|
|
QMutex SearchManager::m_mutexFile;
|
|
|
|
|
QMutex SearchManager::m_mutexDir;
|
|
|
|
|
QMutex SearchManager::m_mutexContent;
|
2022-01-21 16:53:19 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
SearchManager::SearchManager(QObject *parent) : QObject(parent) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
SearchManager::~SearchManager() {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
uint SearchManager::getCurrentIndexCount() {
|
2021-04-16 15:35:54 +08:00
|
|
|
|
try {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Database db(INDEX_PATH);
|
|
|
|
|
return db.get_doccount();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} catch(const Xapian::Error &e) {
|
|
|
|
|
qWarning() << QString::fromStdString(e.get_description());
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
bool SearchManager::isBlocked(QString &path) {
|
2023-05-06 09:19:50 +08:00
|
|
|
|
QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser();
|
2023-09-28 10:54:58 +08:00
|
|
|
|
for(const QString& i : blockList) {
|
2021-07-06 16:53:32 +08:00
|
|
|
|
if(FileUtils::isOrUnder(path, i))
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
bool SearchManager::creatResultInfo(SearchPluginIface::ResultInfo &ri, const QString& path)
|
2021-05-27 21:10:11 +08:00
|
|
|
|
{
|
2021-05-28 10:16:28 +08:00
|
|
|
|
QFileInfo info(path);
|
2021-05-27 21:10:11 +08:00
|
|
|
|
if(!info.exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-08-06 10:54:48 +08:00
|
|
|
|
ri.icon = FileUtils::getFileIcon(QUrl::fromLocalFile(path).toString(), false);
|
2023-12-20 15:51:38 +08:00
|
|
|
|
ri.name = info.fileName().replace("\r", " ").replace("\n", " ");
|
|
|
|
|
ri.toolTip = info.fileName();
|
2024-03-05 16:15:42 +08:00
|
|
|
|
ri.resourceType = QStringLiteral("file");
|
2021-05-28 10:16:28 +08:00
|
|
|
|
ri.description = QVector<SearchPluginIface::DescriptionInfo>() \
|
|
|
|
|
<< SearchPluginIface::DescriptionInfo{tr("Path:"), path} \
|
|
|
|
|
<< SearchPluginIface::DescriptionInfo{tr("Modified time:"), info.lastModified().toString("yyyy/MM/dd hh:mm:ss")};
|
2021-05-27 21:10:11 +08:00
|
|
|
|
ri.actionKey = path;
|
2024-01-23 11:18:55 +08:00
|
|
|
|
if (FileIndexerConfig::getInstance()->ocrContentIndexTarget()[info.suffix()]) {
|
2022-04-13 13:46:30 +08:00
|
|
|
|
ri.type = 1;//1为ocr图片文件
|
|
|
|
|
} else {
|
|
|
|
|
ri.type = 0;//0为默认文本文件
|
|
|
|
|
}
|
2021-05-27 21:10:11 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-01-21 16:53:19 +08:00
|
|
|
|
|
2021-05-27 21:10:11 +08:00
|
|
|
|
FileSearch::FileSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, QString value, unsigned slot, int begin, int num) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
this->setAutoDelete(true);
|
|
|
|
|
m_search_result = searchResult;
|
|
|
|
|
m_uniqueSymbol = uniqueSymbol;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_keyword = std::move(keyword);
|
|
|
|
|
m_value = std::move(value);
|
2021-03-17 10:23:21 +08:00
|
|
|
|
m_slot = slot;
|
|
|
|
|
m_begin = begin;
|
|
|
|
|
m_num = num;
|
2021-11-10 10:20:16 +08:00
|
|
|
|
m_matchDecider = new FileMatchDecider();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
FileSearch::~FileSearch() {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
m_search_result = nullptr;
|
2021-11-10 10:20:16 +08:00
|
|
|
|
if(m_matchDecider)
|
|
|
|
|
delete m_matchDecider;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void FileSearch::run() {
|
2021-08-24 17:14:28 +08:00
|
|
|
|
if(m_value == "0") {
|
|
|
|
|
SearchManager::m_mutexFile.lock();
|
|
|
|
|
if(!m_search_result->isEmpty()) {
|
|
|
|
|
m_search_result->clear();
|
|
|
|
|
}
|
|
|
|
|
SearchManager::m_mutexFile.unlock();
|
|
|
|
|
} else if(m_value == "1") {
|
|
|
|
|
SearchManager::m_mutexDir.lock();
|
|
|
|
|
if(!m_search_result->isEmpty()) {
|
|
|
|
|
m_search_result->clear();
|
|
|
|
|
}
|
|
|
|
|
SearchManager::m_mutexDir.unlock();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
2021-11-11 11:44:24 +08:00
|
|
|
|
//目前的需求是文件搜索数量无上限。
|
|
|
|
|
//但如果不设置单次搜索数量限制,在一些性能非常弱的机器上(如兆芯某些机器),就算我们这里不阻塞UI,也会因为搜索本身占用cpu过多(可能)导致UI卡顿。
|
|
|
|
|
//可能会有更好的方法,待优化。
|
2021-11-10 10:20:16 +08:00
|
|
|
|
m_begin = 0;
|
2021-11-11 11:44:24 +08:00
|
|
|
|
m_num = 100;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
uint resultCount = 1;
|
|
|
|
|
uint totalCount = 0;
|
2021-11-11 11:44:24 +08:00
|
|
|
|
while(resultCount > 0) {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
resultCount = keywordSearchFile();
|
2021-11-11 11:44:24 +08:00
|
|
|
|
m_begin += m_num;
|
|
|
|
|
totalCount += resultCount;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "Total count:" << m_value << totalCount;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
uint FileSearch::keywordSearchFile() {
|
2021-04-16 15:35:54 +08:00
|
|
|
|
try {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
qDebug() << "--keywordSearchFile start--";
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Database db(INDEX_PATH);
|
2023-04-24 14:07:56 +08:00
|
|
|
|
Xapian::Query query = creatQueryForFileSearch();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Enquire enquire(db);
|
|
|
|
|
|
|
|
|
|
Xapian::Query queryFile;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(!m_value.isEmpty()) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
std::string slotValue = m_value.toStdString();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
Xapian::Query queryValue = Xapian::Query(Xapian::Query::OP_VALUE_RANGE, m_slot, slotValue, slotValue);
|
|
|
|
|
queryFile = Xapian::Query(Xapian::Query::OP_AND, query, queryValue);
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} else {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
queryFile = query;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
qDebug() << "keywordSearchFile:" << QString::fromStdString(queryFile.get_description());
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
|
|
|
|
enquire.set_query(queryFile);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
enquire.set_docid_order(Xapian::Enquire::DONT_CARE);
|
2022-04-19 09:16:20 +08:00
|
|
|
|
enquire.set_sort_by_relevance_then_value(2, true);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
Xapian::MSet result = enquire.get_mset(m_begin, m_num, nullptr, m_matchDecider);
|
|
|
|
|
uint resultCount = result.size();
|
|
|
|
|
qDebug() << "keywordSearchFile results count=" << resultCount;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(resultCount == 0)
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return 0;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(getResult(result) == -1)
|
2023-10-16 13:31:41 +08:00
|
|
|
|
return 0;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
qDebug() << "--keywordSearchFile finish--";
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return resultCount;
|
2021-04-26 15:06:47 +08:00
|
|
|
|
} catch(const Xapian::Error &e) {
|
|
|
|
|
qWarning() << QString::fromStdString(e.get_description());
|
2023-09-28 10:54:58 +08:00
|
|
|
|
qDebug() << "--keywordSearchFile finish--";
|
2023-10-16 13:31:41 +08:00
|
|
|
|
return 0;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-24 14:07:56 +08:00
|
|
|
|
Xapian::Query FileSearch::creatQueryForFileSearch() {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
auto userInput = m_keyword.toLower();
|
|
|
|
|
std::vector<Xapian::Query> v;
|
2023-07-24 11:39:18 +08:00
|
|
|
|
QTextBoundaryFinder bf(QTextBoundaryFinder::Grapheme, userInput);
|
|
|
|
|
int start = 0;
|
|
|
|
|
for(; bf.position() != -1; bf.toNextBoundary()) {
|
|
|
|
|
int end = bf.position();
|
|
|
|
|
if(bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) {
|
2023-09-28 10:54:58 +08:00
|
|
|
|
v.emplace_back(QUrl::toPercentEncoding(userInput.mid(start, end - start)).toStdString());
|
2023-07-24 11:39:18 +08:00
|
|
|
|
}
|
|
|
|
|
start = end;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
2021-04-26 15:06:47 +08:00
|
|
|
|
Xapian::Query queryPhrase = Xapian::Query(Xapian::Query::OP_PHRASE, v.begin(), v.end());
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return queryPhrase;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
int FileSearch::getResult(Xapian::MSet &result) {
|
|
|
|
|
for(auto it = result.begin(); it != result.end(); ++it) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Document doc = it.get_document();
|
|
|
|
|
std::string data = doc.get_data();
|
2022-04-19 09:16:20 +08:00
|
|
|
|
// Xapian::weight docScoreWeight = it.get_weight();
|
|
|
|
|
// Xapian::percent docScorePercent = it.get_percent();
|
|
|
|
|
// std::string date = doc.get_value(2);
|
|
|
|
|
|
2021-03-17 10:23:21 +08:00
|
|
|
|
QString path = QString::fromStdString(data);
|
|
|
|
|
std::string().swap(data);
|
|
|
|
|
|
2021-05-27 21:10:11 +08:00
|
|
|
|
SearchPluginIface::ResultInfo ri;
|
|
|
|
|
if(SearchManager::creatResultInfo(ri, path)) {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
switch(m_value.toInt()) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
case 1:
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexDir.lock();
|
|
|
|
|
if(m_uniqueSymbol == SearchManager::uniqueSymbolDir) {
|
2021-05-27 21:10:11 +08:00
|
|
|
|
m_search_result->enqueue(ri);
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexDir.unlock();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} else {
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexDir.unlock();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 0:
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexFile.lock();
|
|
|
|
|
if(m_uniqueSymbol == SearchManager::uniqueSymbolFile) {
|
2021-05-27 21:10:11 +08:00
|
|
|
|
m_search_result->enqueue(ri);
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexFile.unlock();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} else {
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexFile.unlock();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-19 09:16:20 +08:00
|
|
|
|
// qDebug() << "doc=" << path
|
|
|
|
|
// << ",weight="
|
|
|
|
|
// << docScoreWeight
|
|
|
|
|
// << ",percent="
|
|
|
|
|
// << docScorePercent
|
|
|
|
|
// << "date"
|
|
|
|
|
// << QString::fromStdString(date);
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
// if(!pathTobeDelete->isEmpty())
|
|
|
|
|
// deleteAllIndex(pathTobeDelete)
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
FileContentSearch::FileContentSearch(DataQueue<SearchPluginIface::ResultInfo> *searchResult, size_t uniqueSymbol, QString keyword, bool fuzzy, int begin, int num)
|
|
|
|
|
:m_search_result(searchResult),
|
|
|
|
|
m_uniqueSymbol(uniqueSymbol),
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_keyword(std::move(keyword)),
|
2022-11-28 18:11:55 +08:00
|
|
|
|
m_fuzzy(fuzzy),
|
|
|
|
|
m_begin(begin),
|
|
|
|
|
m_num(num)
|
|
|
|
|
{
|
2021-03-17 10:23:21 +08:00
|
|
|
|
this->setAutoDelete(true);
|
2021-11-10 10:20:16 +08:00
|
|
|
|
m_matchDecider = new FileContentMatchDecider();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
FileContentSearch::~FileContentSearch() {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
m_search_result = nullptr;
|
2021-11-10 10:20:16 +08:00
|
|
|
|
if(m_matchDecider)
|
|
|
|
|
delete m_matchDecider;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void FileContentSearch::run() {
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexContent.lock();
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(!m_search_result->isEmpty()) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
m_search_result->clear();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexContent.unlock();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
2021-11-11 11:44:24 +08:00
|
|
|
|
//这里同文件搜索,待优化。
|
2021-11-10 10:20:16 +08:00
|
|
|
|
m_begin = 0;
|
2021-11-11 11:44:24 +08:00
|
|
|
|
m_num = 100;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
uint resultCount = 1;
|
|
|
|
|
uint totalCount = 0;
|
2021-11-11 11:44:24 +08:00
|
|
|
|
while(resultCount > 0) {
|
|
|
|
|
resultCount = keywordSearchContent();
|
|
|
|
|
m_begin += m_num;
|
|
|
|
|
totalCount += resultCount;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "Total count:" << totalCount;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
uint FileContentSearch::keywordSearchContent() {
|
2021-04-16 15:35:54 +08:00
|
|
|
|
try {
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "--keywordSearchContent search start--";
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
|
|
|
|
Xapian::Database db(CONTENT_INDEX_PATH);
|
2023-12-15 13:43:42 +08:00
|
|
|
|
if(FileIndexerConfig::getInstance()->isOCREnable()) {
|
|
|
|
|
db.add_database(Xapian::Database(OCR_CONTENT_INDEX_PATH));
|
|
|
|
|
}
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Enquire enquire(db);
|
|
|
|
|
Xapian::QueryParser qp;
|
|
|
|
|
qp.set_default_op(Xapian::Query::OP_AND);
|
|
|
|
|
qp.set_database(db);
|
|
|
|
|
|
2023-03-20 15:21:58 +08:00
|
|
|
|
std::vector<KeyWord> sKeyWord = ChineseSegmentation::getInstance()->callSegment(m_keyword);
|
2021-03-17 10:23:21 +08:00
|
|
|
|
//Creat a query
|
|
|
|
|
std::string words;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
for(auto & i : sKeyWord) {
|
|
|
|
|
words.append(i.word).append(" ");
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Xapian::Query query = qp.parse_query(words);
|
|
|
|
|
|
2021-05-28 14:22:17 +08:00
|
|
|
|
std::vector<Xapian::Query> v;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
for(auto & i : sKeyWord) {
|
|
|
|
|
v.emplace_back(i.word);
|
2022-11-28 18:11:55 +08:00
|
|
|
|
// qDebug() << QString::fromStdString(sKeyWord.at(i).word);
|
|
|
|
|
}
|
|
|
|
|
Xapian::Query query;
|
|
|
|
|
if(m_fuzzy) {
|
|
|
|
|
query = Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
|
|
|
|
|
} else {
|
|
|
|
|
query = Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end());
|
2021-05-28 14:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 15:35:54 +08:00
|
|
|
|
qDebug() << "keywordSearchContent:" << QString::fromStdString(query.get_description());
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
|
|
|
|
enquire.set_query(query);
|
|
|
|
|
|
2023-09-28 10:54:58 +08:00
|
|
|
|
Xapian::MSet result = enquire.get_mset(m_begin, m_num, nullptr, m_matchDecider);
|
|
|
|
|
uint resultCount = result.size();
|
|
|
|
|
if(result.empty()) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return 0;
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
qDebug() << "keywordSearchContent results count=" << resultCount;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
if(getResult(result, words) == -1) {
|
2023-10-16 13:31:41 +08:00
|
|
|
|
return 0;
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
qDebug() << "--keywordSearchContent search finish--";
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return resultCount;
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} catch(const Xapian::Error &e) {
|
|
|
|
|
qWarning() << QString::fromStdString(e.get_description());
|
|
|
|
|
qDebug() << "--keywordSearchContent search finish--";
|
2023-10-16 13:31:41 +08:00
|
|
|
|
return 0;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
int FileContentSearch::getResult(Xapian::MSet &result, std::string &keyWord) {
|
|
|
|
|
for(auto it = result.begin(); it != result.end(); ++it) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
Xapian::Document doc = it.get_document();
|
|
|
|
|
std::string data = doc.get_data();
|
2022-11-28 18:11:55 +08:00
|
|
|
|
// double docScoreWeight = it.get_weight();
|
|
|
|
|
// Xapian::percent docScorePercent = it.get_percent();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
QString path = QString::fromStdString(doc.get_value(1));
|
2022-11-28 18:11:55 +08:00
|
|
|
|
// QString suffix = QString::fromStdString(doc.get_value(2));
|
2021-03-17 10:23:21 +08:00
|
|
|
|
|
2021-05-27 21:10:11 +08:00
|
|
|
|
SearchPluginIface::ResultInfo ri;
|
|
|
|
|
if(!SearchManager::creatResultInfo(ri, path)) {
|
2021-03-17 10:23:21 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Construct snippets containing keyword.
|
2022-11-28 18:11:55 +08:00
|
|
|
|
auto termIterator = doc.termlist_begin();
|
2023-09-28 10:54:58 +08:00
|
|
|
|
QStringList words = QString::fromStdString(keyWord).split(" ", Qt::SkipEmptyParts);
|
2021-08-06 17:45:28 +08:00
|
|
|
|
|
2022-11-28 18:11:55 +08:00
|
|
|
|
for(const QString& wordTobeFound : words) {
|
|
|
|
|
std::string term = wordTobeFound.toStdString();
|
|
|
|
|
termIterator.skip_to(term);
|
2022-11-30 16:19:18 +08:00
|
|
|
|
if(termIterator == doc.termlist_end()) {
|
|
|
|
|
termIterator = doc.termlist_begin();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-28 18:11:55 +08:00
|
|
|
|
if(term == *termIterator) {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
termIterator = doc.termlist_begin();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
auto pos = termIterator.positionlist_begin();
|
2023-07-27 13:59:21 +08:00
|
|
|
|
QString snippet = FileUtils::getSnippet(data, *pos, QString::fromStdString(keyWord).remove(" "));
|
2021-07-31 16:12:04 +08:00
|
|
|
|
|
2023-07-18 16:18:58 +08:00
|
|
|
|
ri.description.prepend(SearchPluginIface::DescriptionInfo{"",FileUtils::getHtmlText(snippet, QString::fromStdString(keyWord).remove(" "))});
|
2021-07-31 16:12:04 +08:00
|
|
|
|
QString().swap(snippet);
|
2021-03-17 10:23:21 +08:00
|
|
|
|
std::string().swap(data);
|
|
|
|
|
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexContent.lock();
|
|
|
|
|
if(m_uniqueSymbol == SearchManager::uniqueSymbolContent) {
|
2021-05-27 21:10:11 +08:00
|
|
|
|
m_search_result->enqueue(ri);
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexContent.unlock();
|
2021-04-16 15:35:54 +08:00
|
|
|
|
} else {
|
2021-08-24 17:14:28 +08:00
|
|
|
|
SearchManager::m_mutexContent.unlock();
|
2021-03-17 10:23:21 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-08-03 16:08:30 +08:00
|
|
|
|
//qDebug() << "path=" << path << ",weight=" << docScoreWeight << ",percent=" << docScorePercent;
|
2021-03-17 10:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-04-16 15:35:54 +08:00
|
|
|
|
|
2021-05-27 21:10:11 +08:00
|
|
|
|
DirectSearch::DirectSearch(QString keyword, DataQueue<SearchPluginIface::ResultInfo> *searchResult, QString value, size_t uniqueSymbol) {
|
2021-04-16 15:35:54 +08:00
|
|
|
|
this->setAutoDelete(true);
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_keyword = std::move(keyword);
|
2021-05-27 21:10:11 +08:00
|
|
|
|
m_searchResult = searchResult;
|
2021-04-16 15:35:54 +08:00
|
|
|
|
m_uniqueSymbol = uniqueSymbol;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
m_value = std::move(value);
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 15:06:47 +08:00
|
|
|
|
void DirectSearch::run() {
|
2023-05-06 09:19:50 +08:00
|
|
|
|
QStringList blockList = DirWatcher::getDirWatcher()->getBlockDirsOfUser();
|
2022-12-07 14:04:21 +08:00
|
|
|
|
QStringList searchPath = DirWatcher::getDirWatcher()->currentIndexableDir();
|
2022-04-08 11:15:18 +08:00
|
|
|
|
QQueue<QString> bfs;
|
|
|
|
|
for (const QString &path : searchPath) {
|
2023-04-12 15:05:25 +08:00
|
|
|
|
bool underBlock(false);
|
|
|
|
|
for (const QString &blockDir : blockList) {
|
|
|
|
|
if (FileUtils::isOrUnder(path, blockDir)) {
|
|
|
|
|
underBlock = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!underBlock) {
|
|
|
|
|
blockList.append(DirWatcher::getDirWatcher()->blackListOfDir(path));
|
|
|
|
|
bfs.enqueue(path);
|
2023-04-21 11:22:41 +08:00
|
|
|
|
match(QFileInfo(path));
|
2022-04-08 11:15:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (bfs.isEmpty()) {
|
2021-09-15 14:47:08 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-08 11:15:18 +08:00
|
|
|
|
|
2021-04-16 15:35:54 +08:00
|
|
|
|
QFileInfoList list;
|
|
|
|
|
QDir dir;
|
|
|
|
|
// QDir::Hidden
|
2021-05-27 21:10:11 +08:00
|
|
|
|
if(m_value == DIR_SEARCH_VALUE) {
|
|
|
|
|
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
} else {
|
|
|
|
|
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
|
dir.setSorting(QDir::DirsFirst);
|
|
|
|
|
}
|
2021-04-26 15:06:47 +08:00
|
|
|
|
while(!bfs.empty()) {
|
2021-04-16 15:35:54 +08:00
|
|
|
|
dir.setPath(bfs.dequeue());
|
|
|
|
|
list = dir.entryInfoList();
|
2021-05-08 11:02:13 +08:00
|
|
|
|
for (auto i : list) {
|
|
|
|
|
if (i.isDir() && (!(i.isSymLink()))) {
|
2021-05-08 09:26:12 +08:00
|
|
|
|
bool findIndex = false;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
for (const QString& j : blockList) {
|
2021-07-06 16:53:32 +08:00
|
|
|
|
if (FileUtils::isOrUnder(i.absoluteFilePath(), j)) {
|
2021-05-08 09:26:12 +08:00
|
|
|
|
findIndex = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-28 10:54:58 +08:00
|
|
|
|
if (findIndex) {
|
2021-05-08 09:26:12 +08:00
|
|
|
|
qDebug() << "path is blocked:" << i.absoluteFilePath();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-16 15:35:54 +08:00
|
|
|
|
bfs.enqueue(i.absoluteFilePath());
|
|
|
|
|
}
|
2023-06-14 14:08:55 +08:00
|
|
|
|
SearchManager::m_mutexDir.lock();
|
|
|
|
|
if(m_uniqueSymbol == SearchManager::uniqueSymbolDir) {
|
|
|
|
|
match(i);
|
|
|
|
|
SearchManager::m_mutexDir.unlock();
|
|
|
|
|
} else {
|
|
|
|
|
SearchManager::m_mutexDir.unlock();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-21 11:22:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DirectSearch::match(const QFileInfo &info)
|
|
|
|
|
{
|
|
|
|
|
if(info.fileName().contains(m_keyword, Qt::CaseInsensitive)) {
|
2023-06-14 14:08:55 +08:00
|
|
|
|
if((info.isDir() && m_value == DIR_SEARCH_VALUE) || (info.isFile() && m_value == FILE_SEARCH_VALUE)) {
|
2023-04-21 11:22:41 +08:00
|
|
|
|
SearchPluginIface::ResultInfo ri;
|
|
|
|
|
if(SearchManager::creatResultInfo(ri,info.absoluteFilePath())) {
|
2023-06-14 14:08:55 +08:00
|
|
|
|
m_searchResult->enqueue(ri);
|
2021-04-16 15:35:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-10 10:20:16 +08:00
|
|
|
|
|
|
|
|
|
bool FileMatchDecider::operator ()(const Xapian::Document &doc) const
|
|
|
|
|
{
|
|
|
|
|
QString path = QString::fromStdString(doc.get_data());
|
|
|
|
|
if(SearchManager::isBlocked(path)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileContentMatchDecider::operator ()(const Xapian::Document &doc) const
|
|
|
|
|
{
|
|
|
|
|
QString path = QString::fromStdString(doc.get_value(1));
|
|
|
|
|
if(SearchManager::isBlocked(path)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2023-09-28 10:54:58 +08:00
|
|
|
|
}
|