Add slots and signals for file search.

This commit is contained in:
zhangpengfei 2020-12-26 14:10:14 +08:00
parent 05eb521213
commit 4ba2d53cf6
5 changed files with 117 additions and 9 deletions

View File

@ -11,9 +11,9 @@ Document::~Document()
// if(m_document)
// delete m_document;
// if(m_index_text)
// delete m_index_text;
// delete m_index_text;
// if(m_unique_term)
// delete m_unique_term;
// delete m_unique_term;
}
void Document::setData(QString data)

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)
{
qDebug()<< "--index start--";
// qDebug()<< "--index start--";
Xapian::Document document = doc.getXapianDocument();
m_indexer->set_document(document);
qDebug()<<doc.getIndexText();
// qDebug()<<doc.getIndexText();
for(auto i : doc.getIndexText()){
m_indexer->index_text(i.toStdString());
}
Xapian::docid innerId= m_datebase->replace_document(doc.getUniqueTerm(),document);
qDebug()<<"replace doc docid="<<static_cast<int>(innerId);
qDebug()<< "--index finish--";
// qDebug()<<"replace doc docid="<<static_cast<int>(innerId);
// qDebug()<< "--index finish--";
return;
}
void IndexGenerator::HandlePathList(QList<QVector<QString>> *messageList)
{
qDebug()<<"Begin HandlePathList!";
qDebug()<<messageList->size();
// qDebug()<<QString::number(quintptr(QThread::currentThreadId()));
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++)
{
QString doc = list->at(i);
std::string uniqueterm = m_cryp->hash(doc.toUtf8(),QCryptographicHash::Md5).toStdString();;
std::string uniqueterm = FileUtils::makeDocUterm(doc);
try
{
qDebug()<<"--delete start--";

View File

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