diff --git a/libsearch/index/file-indexer-config.cpp b/libsearch/index/file-indexer-config.cpp index 1b54317..1600330 100644 --- a/libsearch/index/file-indexer-config.cpp +++ b/libsearch/index/file-indexer-config.cpp @@ -22,6 +22,7 @@ #include #include #include +#include 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 FileIndexerConfig::contentIndexTarget() { + QMutexLocker locker(&m_mutex); return m_targetFileTypeMap; } QMap 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()) { diff --git a/libsearch/index/file-indexer-config.h b/libsearch/index/file-indexer-config.h index 75699d9..542dc3a 100644 --- a/libsearch/index/file-indexer-config.h +++ b/libsearch/index/file-indexer-config.h @@ -23,6 +23,7 @@ #include #include #include +#include #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 diff --git a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp index 2c94abb..9ca086d 100644 --- a/libsearch/searchinterface/searchtasks/file-content-search-task.cpp +++ b/libsearch/searchinterface/searchtasks/file-content-search-task.cpp @@ -25,6 +25,7 @@ #include "result-item.h" #include "common.h" #include "file-utils.h" +#include "file-indexer-config.h" //Qt #include @@ -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; }