Modified code about index creation.

This commit is contained in:
zhangpengfei 2020-12-22 21:20:47 +08:00 committed by Burgess Chang
parent 468212e9d8
commit 43a423d0e1
5 changed files with 52 additions and 43 deletions

View File

@ -16,6 +16,11 @@ IndexGenerator *IndexGenerator::getInstance()
return global_instance; return global_instance;
} }
bool IndexGenerator::setIndexdataPath()
{
return true;
}
bool IndexGenerator::creatAllIndex(QStringList *pathlist) bool IndexGenerator::creatAllIndex(QStringList *pathlist)
{ {
HandlePathList(pathlist); HandlePathList(pathlist);
@ -23,7 +28,7 @@ bool IndexGenerator::creatAllIndex(QStringList *pathlist)
{ {
m_indexer = new Xapian::TermGenerator(); m_indexer = new Xapian::TermGenerator();
m_indexer->set_database(*m_datebase); m_indexer->set_database(*m_datebase);
m_indexer->set_flags(Xapian::TermGenerator::FLAG_SPELLING); // m_indexer->set_flags(Xapian::TermGenerator::FLAG_SPELLING);
m_indexer->set_stemming_strategy(Xapian::TermGenerator::STEM_SOME); m_indexer->set_stemming_strategy(Xapian::TermGenerator::STEM_SOME);
QMap<QString, QString>::const_iterator i; QMap<QString, QString>::const_iterator i;
@ -33,7 +38,7 @@ bool IndexGenerator::creatAllIndex(QStringList *pathlist)
{ {
docStr = new QString(i.key()); docStr = new QString(i.key());
indexStr = new QString(i.value()); indexStr = new QString(i.value());
CreatIndex(indexStr,docStr); insertIntoDatabase(indexStr,docStr);
} }
m_datebase->commit(); m_datebase->commit();
} }
@ -58,7 +63,7 @@ IndexGenerator::~IndexGenerator()
{ {
} }
void IndexGenerator::CreatIndex(QString *indexText,QString *doc) void IndexGenerator::insertIntoDatabase(QString *indexText,QString *doc)
{ {
qDebug()<< "--index start--"; qDebug()<< "--index start--";
m_docstr = doc->toStdString(); m_docstr = doc->toStdString();
@ -92,7 +97,8 @@ void IndexGenerator::HandlePathList(QStringList *pathlist)
//提取文件名并用空格分割,同时去除'.' //提取文件名并用空格分割,同时去除'.'
QString filename = info->fileName(); QString filename = info->fileName();
QString index_test = filename.replace(".","").replace(""," "); QString index_test = filename.replace(".","").replace(""," ");
index_test.simplified();
// index_text.simplified();
// qDebug()<<"index_test"<<index_test; // qDebug()<<"index_test"<<index_test;
m_index_map->insert(info->absoluteFilePath(),index_test); m_index_map->insert(info->absoluteFilePath(),index_test);
// qDebug()<<m_index_map->value(index_test); // qDebug()<<m_index_map->value(index_test);
@ -110,30 +116,46 @@ bool IndexGenerator::isIndexdataExist()
} }
void IndexGenerator::IndexSearch(QString *indexText) QStringList IndexGenerator::IndexSearch(QString indexText)
{ {
QStringList searchResult;
try try
{ {
qDebug()<<"--search start--"; qDebug()<<"--search start--";
Xapian::Enquire enquire(*m_datebase); Xapian::Database db(INDEX_PATH);
Xapian::Enquire enquire(db);
Xapian::QueryParser qp; Xapian::QueryParser qp;
qp.set_default_op(Xapian::Query::OP_NEAR); qp.set_default_op(Xapian::Query::OP_PHRASE);
qp.set_database(*m_datebase); qp.set_database(db);
auto userInput = indexText;
std::string queryStr = indexText->replace(""," ").toStdString(); std::string queryStr = indexText.replace(""," ").toStdString();
// std::string s =db.get_spelling_suggestion(queryStr,10); // std::string s =db.get_spelling_suggestion(queryStr,10);
// qDebug()<<"spelling_suggestion!"<<QString::fromStdString(s); // qDebug()<<"spelling_suggestion!"<<QString::fromStdString(s);
qDebug()<<"queryStr!"<<QString::fromStdString(queryStr); qDebug()<<"queryStr!"<<QString::fromStdString(queryStr);
Xapian::Query query = qp.parse_query(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()); qDebug()<<QString::fromStdString(query.get_description());
enquire.set_query(query); enquire.set_query(query);
Xapian::MSet result = enquire.get_mset(0, 9999); Xapian::MSet result = enquire.get_mset(0, 9999);
qDebug()<< "find results count=" <<static_cast<int>(result.get_matches_estimated()); qDebug()<< "find results count=" <<static_cast<int>(result.get_matches_estimated());
QStringList *pathTobeDelete = new QStringList; // QStringList *pathTobeDelete = new QStringList;
//get search result
for (auto it = result.begin(); it != result.end(); ++it) { for (auto it = result.begin(); it != result.end(); ++it) {
Xapian::Document doc = it.get_document(); Xapian::Document doc = it.get_document();
std::string data = doc.get_data(); std::string data = doc.get_data();
@ -143,15 +165,19 @@ void IndexGenerator::IndexSearch(QString *indexText)
if(!info->exists()) if(!info->exists())
{ {
pathTobeDelete->append(QString::fromStdString(data)); // pathTobeDelete->append(QString::fromStdString(data));
qDebug()<<QString::fromStdString(data)<<"is not exist!!"; qDebug()<<QString::fromStdString(data)<<"is not exist!!";
} }
else
{
searchResult.append(QString::fromStdString(data));
}
qDebug()<< "doc="<< QString::fromStdString(data) << ",weight=" <<docScoreWeight << ",percent=" << docScorePercent; qDebug()<< "doc="<< QString::fromStdString(data) << ",weight=" <<docScoreWeight << ",percent=" << docScorePercent;
} }
//Delete those path doc which is not already exist. // //Delete those path doc which is not already exist.
if(!pathTobeDelete->isEmpty()) // if(!pathTobeDelete->isEmpty())
deleteAllIndex(pathTobeDelete); // deleteAllIndex(pathTobeDelete);
qDebug()<< "--search finish--"; qDebug()<< "--search finish--";
} }
@ -159,8 +185,7 @@ void IndexGenerator::IndexSearch(QString *indexText)
{ {
qDebug() <<QString::fromStdString(e.get_description()); qDebug() <<QString::fromStdString(e.get_description());
} }
Q_EMIT this->searchFinish(); return searchResult;
return;
} }
bool IndexGenerator::deleteAllIndex(QStringList *pathlist) bool IndexGenerator::deleteAllIndex(QStringList *pathlist)

View File

@ -5,7 +5,7 @@
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QMultiMap> #include <QMap>
#include <QCryptographicHash> #include <QCryptographicHash>
class IndexGenerator : public QObject class IndexGenerator : public QObject
@ -13,19 +13,21 @@ class IndexGenerator : public QObject
Q_OBJECT Q_OBJECT
public: public:
static IndexGenerator *getInstance(); static IndexGenerator *getInstance();
bool setIndexdataPath();
bool isIndexdataExist(); bool isIndexdataExist();
static QStringList IndexSearch(QString indexText);
Q_SIGNALS: Q_SIGNALS:
void transactionFinished(); void transactionFinished();
void searchFinish(); void searchFinish();
public Q_SLOTS: public Q_SLOTS:
bool creatAllIndex(QStringList *pathlist); bool creatAllIndex(QStringList *pathlist);
bool deleteAllIndex(QStringList *pathlist); bool deleteAllIndex(QStringList *pathlist);
void IndexSearch(QString *indexText);
private: private:
explicit IndexGenerator(QObject *parent = nullptr); explicit IndexGenerator(QObject *parent = nullptr);
void HandlePathList(QStringList *pathlist); void HandlePathList(QStringList *pathlist);
//add one data in database //add one data in database
void CreatIndex(QString *indexText,QString *doc); void insertIntoDatabase(QString *indexText,QString *doc);
~IndexGenerator(); ~IndexGenerator();
QMap<QString,QString> *m_index_map; QMap<QString,QString> *m_index_map;

View File

@ -1,30 +1,9 @@
#include "mainwindow.h" #include "src/mainwindow.h"
#include "inotify-manager.h" #include "inotify-manager.h"
#include <QTime> #include <QTime>
#include <QDebug> #include <QDebug>
void testTraverse(void){ void testTraverse(void){
// QStringList qsl;
// for (int i = 0; i < 4000; i++){
// qsl.append(QString("%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1%1").arg(i));
// }
// for (int i = 0; i < 4000; i++){
// qDebug() << i << " " << qsl.at(i);
// if (qsl.at(i) != QString("%1").arg(i)){
// qDebug() << "fuck stringlist";
// }
// }
// qDebug() << qsl.length();
// qDebug() << "stringlist";
// exit(0);
QTime t1 = QTime::currentTime(); QTime t1 = QTime::currentTime();
InotifyManager* im = new InotifyManager(); InotifyManager* im = new InotifyManager();
im->AddWatch("/home/zpf"); im->AddWatch("/home/zpf");

View File

@ -29,6 +29,9 @@
#include <KWindowEffects> #include <KWindowEffects>
#include "kwindowsystem.h" #include "kwindowsystem.h"
#include "file-utils.h"
#include "index-generator.h"
#include "inotify-manager.h"
extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed); extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);
/** /**

View File

@ -15,7 +15,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
# You can also select to disable deprecated APIs only up to a certain version of Qt. # You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
include(src/src.pri)) include(src/src.pri)
include(index/index.pri) include(index/index.pri)
include(model/model.pri) include(model/model.pri)
include(control/control.pri)) include(control/control.pri))