#include #include #include #include #include "file-utils.h" #include "index-generator.h" #include "chinesecharacterstopinyin.h" #include #include using namespace std; #define INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/index_data").toStdString() #define CONTENT_INDEX_PATH (QStandardPaths::writableLocation(QStandardPaths::HomeLocation)+"/.config/org.ukui/content_index_data").toStdString() static IndexGenerator *global_instance = nullptr; IndexGenerator *IndexGenerator::getInstance() { if (!global_instance) { global_instance = new IndexGenerator; } return global_instance; } bool IndexGenerator::setIndexdataPath() { return true; } bool IndexGenerator::creatAllIndex(QList > *messageList) { HandlePathList(messageList); try { m_indexer = new Xapian::TermGenerator(); m_indexer->set_database(*m_datebase_path); //可以实现拼写纠正 // m_indexer->set_flags(Xapian::TermGenerator::FLAG_SPELLING); m_indexer->set_stemming_strategy(Xapian::TermGenerator::STEM_SOME); int count =0; for(int i = 0;i < m_doc_list_path->size(); i++) { insertIntoDatabase(m_doc_list_path->at(i)); if(++count == 9999) { count = 0; m_datebase_path->commit(); } } m_datebase_path->commit(); } catch(const Xapian::Error &e) { qDebug()<<"creatAllIndex fail!"<clear(); Q_EMIT this->transactionFinished(); return true; } bool IndexGenerator::creatAllIndex(QVector *messageList) { HandlePathList(messageList); return true; } IndexGenerator::IndexGenerator(QObject *parent) : QObject(parent) { m_datebase_path = new Xapian::WritableDatabase(INDEX_PATH, Xapian::DB_CREATE_OR_OPEN); m_database_content = new Xapian::WritableDatabase(CONTENT_INDEX_PATH, Xapian::DB_CREATE_OR_OPEN); } IndexGenerator::~IndexGenerator() { } void IndexGenerator::insertIntoDatabase(Document doc) { // qDebug()<< "--index start--"; Xapian::Document document = doc.getXapianDocument(); m_indexer->set_document(document); // qDebug()<index_text(i.toStdString()); } Xapian::docid innerId= m_datebase_path->replace_document(doc.getUniqueTerm(),document); // qDebug()<<"replace doc docid="<(innerId); // qDebug()<< "--index finish--"; return; } void IndexGenerator::HandlePathList(QList> *messageList) { qDebug()<<"Begin HandlePathList!"; qDebug()<size(); // qDebug()< future = QtConcurrent::mapped(*messageList,&IndexGenerator::GenerateDocument); future.waitForFinished(); QList docList = future.results(); m_doc_list_path = new QList(docList); qDebug()<size(); qDebug()<<"Finish HandlePathList!"; return; } void IndexGenerator::HandlePathList(QVector *messageList) { qDebug()<<"Begin HandlePathList for content index!"; qDebug()<size(); // qDebug()< future = QtConcurrent::mapped(*messageList,&IndexGenerator::GenerateContentDocument); future.waitForFinished(); QList docList = future.results(); m_doc_list_content = new QList(docList); qDebug()<size(); qDebug()<<"Finish HandlePathList for content index!"; return; } Document IndexGenerator::GenerateDocument(const QVector &list) { // qDebug()<toStdString()); } QStringList IndexGenerator::IndexSearch(QString indexText) { 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 = indexText; std::string queryStr = indexText.replace(""," ").toStdString(); // std::string s =db.get_spelling_suggestion(queryStr,10); // qDebug()<<"spelling_suggestion!"< v; for(int i=0;iisEmpty()) return true; for(int i = 0;isize();i++) { QString doc = list->at(i); std::string uniqueterm = FileUtils::makeDocUterm(doc); try { qDebug()<<"--delete start--"; m_datebase_path->delete_document(uniqueterm); qDebug()<<"delete md5"<commit(); qDebug()<< "--delete finish--"; } catch(const Xapian::Error &e) { qDebug() <transactionFinished(); return true; }