Merge pull request #15 from iaom/1226-dev

Add slots and signals for file search.
This commit is contained in:
张佳萍 2020-12-26 14:17:24 +08:00 committed by GitHub
commit ca4aab8c05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 9 deletions

81
index/file-searcher.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "file-searcher.h"
FileSearcher::FileSearcher(QObject *parent) : QObject(parent)
{
}
void FileSearcher::onKeywordSearch(QString keyword)
{
QStringList searchResult;
try
{
qDebug()<<"--search start--";
Xapian::Database db(INDEX_PATH);
Xapian::Enquire enquire(db);
Xapian::QueryParser qp;
qp.set_default_op(Xapian::Query::OP_PHRASE);
qp.set_database(db);
auto userInput = keyword;
std::string queryStr = indexText.replace(""," ").toStdString();
// std::string s =db.get_spelling_suggestion(queryStr,10);
// qDebug()<<"spelling_suggestion!"<<QString::fromStdString(s);
qDebug()<<"queryStr!"<<QString::fromStdString(queryStr);
//Creat a query
Xapian::Query queryPhrase = qp.parse_query(queryStr,Xapian::QueryParser::FLAG_PHRASE);
vector<Xapian::Query> v;
for(int i=0;i<userInput.size();i++)
{
v.push_back(Xapian::Query(QString(userInput.at(i)).toStdString()));
qDebug()<<userInput.at(i);
qDebug()<<QString::fromStdString(Xapian::Query(QString(userInput.at(i)).toStdString()).get_description());
}
Xapian::Query queryNear =Xapian::Query(Xapian::Query::OP_NEAR, v.begin(), v.end());
Xapian::Query query = Xapian::Query(Xapian::Query::OP_AND,queryNear,queryPhrase);
qDebug()<<QString::fromStdString(query.get_description());
enquire.set_query(query);
Xapian::MSet result = enquire.get_mset(0, 9999);
qDebug()<< "find results count=" <<static_cast<int>(result.get_matches_estimated());
// QStringList *pathTobeDelete = new QStringList;
//get search result
for (auto it = result.begin(); it != result.end(); ++it) {
Xapian::Document doc = it.get_document();
std::string data = doc.get_data();
Xapian::weight docScoreWeight = it.get_weight();
Xapian::percent docScorePercent = it.get_percent();
QFileInfo *info = new QFileInfo(QString::fromStdString(data));
if(!info->exists())
{
// pathTobeDelete->append(QString::fromStdString(data));
qDebug()<<QString::fromStdString(data)<<"is not exist!!";
}
else
{
searchResult.append(QString::fromStdString(data));
}
qDebug()<< "doc="<< QString::fromStdString(data) << ",weight=" <<docScoreWeight << ",percent=" << docScorePercent;
}
// //Delete those path doc which is not already exist.
// if(!pathTobeDelete->isEmpty())
// deleteAllIndex(pathTobeDelete);
qDebug()<< "--search finish--";
}
catch(const Xapian::Error &e)
{
qDebug() <<QString::fromStdString(e.get_description());
return;
}
Q_EMIT this->result(searchResult);
return;
}

24
index/file-searcher.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef FILESEARCHER_H
#define FILESEARCHER_H
#include <QObject>
#include <xapian.h>
#define INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/index_data").toStdString()
class FileSearcher : public QObject
{
Q_OBJECT
public:
explicit FileSearcher(QObject *parent = nullptr);
public Q_SLOTS:
void onKeywordSearch(QString keyword);
Q_SIGNALS:
void result(QStringList resultList);
};
#endif // FILESEARCHER_H

View File

@ -77,24 +77,25 @@ IndexGenerator::~IndexGenerator()
void IndexGenerator::insertIntoDatabase(Document doc) void IndexGenerator::insertIntoDatabase(Document doc)
{ {
qDebug()<< "--index start--"; // qDebug()<< "--index start--";
Xapian::Document document = doc.getXapianDocument(); Xapian::Document document = doc.getXapianDocument();
m_indexer->set_document(document); m_indexer->set_document(document);
qDebug()<<doc.getIndexText(); // qDebug()<<doc.getIndexText();
for(auto i : doc.getIndexText()){ for(auto i : doc.getIndexText()){
m_indexer->index_text(i.toStdString()); m_indexer->index_text(i.toStdString());
} }
Xapian::docid innerId= m_datebase->replace_document(doc.getUniqueTerm(),document); Xapian::docid innerId= m_datebase->replace_document(doc.getUniqueTerm(),document);
qDebug()<<"replace doc docid="<<static_cast<int>(innerId); // qDebug()<<"replace doc docid="<<static_cast<int>(innerId);
qDebug()<< "--index finish--"; // qDebug()<< "--index finish--";
return; return;
} }
void IndexGenerator::HandlePathList(QList<QVector<QString>> *messageList) void IndexGenerator::HandlePathList(QList<QVector<QString>> *messageList)
{ {
qDebug()<<"Begin HandlePathList!"; qDebug()<<"Begin HandlePathList!";
qDebug()<<messageList->size();
// qDebug()<<QString::number(quintptr(QThread::currentThreadId())); // qDebug()<<QString::number(quintptr(QThread::currentThreadId()));
QFuture<Document> future = QtConcurrent::mapped(*messageList,&IndexGenerator::GenerateDocument); QFuture<Document> future = QtConcurrent::mapped(*messageList,&IndexGenerator::GenerateDocument);
@ -227,7 +228,7 @@ bool IndexGenerator::deleteAllIndex(QStringList *pathlist)
for(int i = 0;i<list->size();i++) for(int i = 0;i<list->size();i++)
{ {
QString doc = list->at(i); QString doc = list->at(i);
std::string uniqueterm = m_cryp->hash(doc.toUtf8(),QCryptographicHash::Md5).toStdString();; std::string uniqueterm = FileUtils::makeDocUterm(doc);
try try
{ {
qDebug()<<"--delete start--"; qDebug()<<"--delete start--";

View File

@ -9,7 +9,8 @@ HEADERS += \
$$PWD/messagelist-manager.h \ $$PWD/messagelist-manager.h \
$$PWD/traverse_bfs.h \ $$PWD/traverse_bfs.h \
$$PWD/messagelist-manager.h \ $$PWD/messagelist-manager.h \
$$PWD/text-content-indexer.h $$PWD/text-content-indexer.h \
$$PWD/file-searcher.h
SOURCES += \ SOURCES += \
# $$PWD/chinesecharacterstopinyin.cpp \ # $$PWD/chinesecharacterstopinyin.cpp \
@ -20,5 +21,6 @@ SOURCES += \
$$PWD/messagelist-manager.cpp \ $$PWD/messagelist-manager.cpp \
$$PWD/test-Inotify-Manager.cpp \ $$PWD/test-Inotify-Manager.cpp \
$$PWD/traverse_bfs.cpp \ $$PWD/traverse_bfs.cpp \
$$PWD/text-content-indexer.cpp $$PWD/text-content-indexer.cpp \
$$PWD/file-searcher.cpp