fix(search-interface):完善内容搜索接口

This commit is contained in:
iaom 2024-02-22 10:09:47 +08:00
parent cbbd5f980a
commit 815242d090
3 changed files with 29 additions and 8 deletions

View File

@ -22,6 +22,7 @@
#include <mutex>
#include <QDebug>
#include <QDir>
#include <QMutexLocker>
static const QString INDEX_SETTINGS = QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-service.conf";
static const QString CONTENT_INDEX_TARGET_TYPE = "contentIndexTarget";
@ -120,6 +121,7 @@ QStringList FileIndexerConfig::currentBlackListOfIndex()
bool FileIndexerConfig::isFileIndexEnable()
{
QMutexLocker locker(&m_mutex);
if(m_gsettings) {
if(m_gsettings->keys().contains(FILE_INDEX_ENABLE_KEY)) {
return m_gsettings->get(FILE_INDEX_ENABLE_KEY).toBool();
@ -135,6 +137,7 @@ bool FileIndexerConfig::isFileIndexEnable()
bool FileIndexerConfig::isContentIndexEnable()
{
QMutexLocker locker(&m_mutex);
if(m_gsettings) {
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_KEY)) {
return m_gsettings->get(CONTENT_INDEX_ENABLE_KEY).toBool();
@ -150,6 +153,7 @@ bool FileIndexerConfig::isContentIndexEnable()
bool FileIndexerConfig::isFuzzySearchEnable()
{
QMutexLocker locker(&m_mutex);
if(m_gsettings) {
if(m_gsettings->keys().contains(CONTENT_FUZZY_SEARCH_KEY)) {
return m_gsettings->get(CONTENT_FUZZY_SEARCH_KEY).toBool();
@ -165,6 +169,7 @@ bool FileIndexerConfig::isFuzzySearchEnable()
bool FileIndexerConfig::isOCREnable()
{
QMutexLocker locker(&m_mutex);
if(m_gsettings) {
if(m_gsettings->keys().contains(CONTENT_INDEX_ENABLE_OCR_KEY)) {
return m_gsettings->get(CONTENT_INDEX_ENABLE_OCR_KEY).toBool();
@ -185,16 +190,19 @@ bool FileIndexerConfig::isMetaDataIndexEnable()
QMap<QString, bool> FileIndexerConfig::contentIndexTarget()
{
QMutexLocker locker(&m_mutex);
return m_targetFileTypeMap;
}
QMap<QString, bool> FileIndexerConfig::ocrContentIndexTarget()
{
QMutexLocker locker(&m_mutex);
return m_targetPhotographTypeMap;
}
void FileIndexerConfig::sync()
{
QMutexLocker locker(&m_mutex);
m_settings->sync();
m_settings->beginGroup(CONTENT_INDEX_TARGET_TYPE);
if(m_settings->allKeys().isEmpty()) {

View File

@ -23,6 +23,7 @@
#include <QObject>
#include <QSettings>
#include <QGSettings>
#include <QMutex>
#include "dir-watcher.h"
namespace UkuiSearch{
@ -145,7 +146,7 @@ private:
DirWatcher *m_dirWatcher = nullptr;
QGSettings *m_gsettings = nullptr;
QSettings *m_settings = nullptr;
QMutex m_mutex;
};
}
#endif // FILEINDEXERCONFIG_H

View File

@ -25,6 +25,7 @@
#include "result-item.h"
#include "common.h"
#include "file-utils.h"
#include "file-indexer-config.h"
//Qt
#include <QIcon>
@ -79,7 +80,8 @@ void FileContentSearchTask::setEnable(bool enable)
bool FileContentSearchTask::isEnable()
{
return m_enable && IndexStatusRecorder::getInstance()->contentIndexDatabaseEnable();
int state = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE_KEY).toInt();
return m_enable && state == IndexStatusRecorder::State::Ready || state == IndexStatusRecorder::State::Updating;
}
QString FileContentSearchTask::getCustomSearchType()
@ -125,13 +127,12 @@ void FileContentSearchWorker::run()
dir = dir.mid(0, dir.length() - 1);
}
//TODO 指定黑名单
m_validDirectories.append(dir);
}
bool finished = true;
if (IndexStatusRecorder::getInstance()->contentIndexDatabaseEnable()) {
qDebug() << "content index ready";
if (m_fileContentSearchTask->isEnable()) {
qDebug() << "content index enable";
finished = execSearch();
} else {
@ -155,20 +156,25 @@ bool FileContentSearchWorker::execSearch()
{
try {
Xapian::Database db(CONTENT_INDEX_PATH.toStdString());
if(FileIndexerConfig::getInstance()->isOCREnable()) {
db.add_database(Xapian::Database(OCR_CONTENT_INDEX_PATH.toStdString()));
}
Xapian::Enquire enquire(db);
enquire.set_query(createQuery());
FileContentSearchFilter filter(this);
Xapian::MSet result = enquire.get_mset(0, m_searchController->maxResults(), 0, &filter);
Xapian::MSet result = enquire.get_mset(0, m_searchController->maxResults(), nullptr, &filter);
for (auto it = result.begin(); it != result.end(); ++it) {
if (m_searchController->beginSearchIdCheck(m_currentSearchId)) {
QString path = QString::fromStdString(it.get_document().get_value(CONTENT_DATABASE_PATH_SLOT));
SearchResultProperties properties = m_searchController->getResultProperties(SearchProperty::SearchType::File);
SearchResultProperties properties = m_searchController->getResultProperties(SearchProperty::SearchType::FileContent);
ResultItem resultItem(m_currentSearchId);
resultItem.setItemKey(path);
if(properties.contains(SearchProperty::SearchResultProperty::FilePath)) {
resultItem.setValue(SearchProperty::SearchResultProperty::FilePath, path);
@ -217,8 +223,14 @@ inline Xapian::Query FileContentSearchWorker::createQuery()
v.emplace_back(c.word);
}
}
Xapian::Query query;
if(FileIndexerConfig::getInstance()->isFuzzySearchEnable()) {
query = Xapian::Query(Xapian::Query::OP_OR, v.begin(), v.end());
} else {
query = Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end());
}
return {Xapian::Query::OP_AND, v.begin(), v.end()};
return query;
}