《C/C+++ Boost 轻量级搜索引擎实战:架构流程、技术栈与工程落地指南——构造正/倒排索引(中篇)》

《C/C+++ Boost 轻量级搜索引擎实战:架构流程、技术栈与工程落地指南——构造正/倒排索引(中篇)》
前引:这是一个聚焦基础搜索引擎核心工作流的实操项目,基于 C/C++ 技术生态落地:从全网爬虫抓取网页资源,到服务器端完成 “去标签 - 数据清洗 - 索引构建” 的预处理,再通过 HTTP 服务接收客户端请求、检索索引并拼接结果页返回 —— 完整覆盖了轻量级搜索引擎的端到端逻辑。项目采用 C++11、STL、Boost 等核心技术栈,搭配 CentOS 7 云服务器 + GCC 编译环境(或 VS 系列开发工具)部署,既适配后端工程的性能需求,也能通过可选的前端技术(HTML5/JS 等)优化用户交互,是理解搜索引擎底层原理与 C++ 工程实践的典型案例

目录

【一】Jieba分词工具

【二】正/倒排索引结构设计

【三】关键函数设计

(1)由文档ID返回文档内容

(2)由关键字返回倒排拉链

(3)说明

(4)建立索引

(5)建立正排索引

(6)建立倒排索引

【四】单例模式


【一】Jieba分词工具

我们在对使用倒排索引的时候需要用到“关键词”,这个关键词由每个.html文档的标题和内容而来,因此就涉及到分词,所以我们使用 cppjieba 分词工具来完成,下面是上传:

如果需要使用到cppjieba分词工具,可以在gitcode上面直接搜索使用Git命令上传,我这里是本地上传到服务器:

然后对 cppjieba/include/cppjieba和cppjieba/dict分别建立软链接:头文件和词库

我们把cppjieba移动到上级目录,然后更新一下这两个软链接:

【二】正/倒排索引结构设计

//正排结构 typedef struct Forward_index { //文档内容 std::string title; std::string source; std::string chain; //对应正排ID uint64_t doc_id; }Forwardindex; //倒排结构 typedef struct Inverted_index { //对应正排ID int doc_id; //涉及到的关键字 std::string word; //权重 int weight; }Invertedindex; 

正排:根据ID映射对应的文档(标题、内容、URL),这个ID刚好利用vector的下标!

倒排:根据“关键字”映射对应的ID,所以利用unordered_map快速的搜索特性

//正排存储 std::vector<Forwardindex> Forward; typedef std::vector<Invertedindex> Stock_Inverted; //倒排存储 std::unordered_map<std::string,Stock_Inverted> Inverted;

【三】关键函数设计

(1)由文档ID返回文档内容

含义:即正排外部的实现,根据ID返回vector中对应的具体内容

//根据ID返回文档内容 Forwardindex* GetForward_index(const long long& id) { if(id>=Forward.size()) { std::cerr<<"GetForward_index is errno"<<std::endl; return nullptr; } return &Forward[id]; }
(2)由关键字返回倒排拉链

含义:倒排外部实现,根据“关键字”返回对应的ID,即返回对应的vector<Invertedindex>成员

解释:每个关键词涉及到的ID肯定不止一个,所以需要vector<Invertedindex>代表一个关键字涉               及到的所有文档

//根据关键字返回倒排拉链 Stock_Inverted* GetInverted_index(const std::string word) { auto it =Inverted.find(word); if(it == Inverted.end()) { std::cerr<<"GetInverted_index is errno"<<std::endl; return {}; } return &it->second; }
(3)说明

现在我们已经有了给外部的接口:由ID返回文档内容、由关键词返回文档ID

那么接下来就是填变量、建立索引关系:

//正排存储 std::vector<Forwardindex> Forward; typedef std::vector<Invertedindex> Stock_Inverted; //倒排存储 std::unordered_map<std::string,Stock_Inverted> Inverted;
(4)建立索引

原理:利用上一篇文章完成的数据清洗,最终每个.html文档内容都被输出到了data_clean.txt中

我们先利用getline按行读取(也就是按单个文档读取,因为data_clean.txt中也是按行写的)到 line变量里面,这样就拿到了单个文档的内容,因此while就是拿取data_clean.txt的全部文档内容!

建索引也就是同时建立正排和倒排的内容,也就是填vector和Unordered_map的操作!

//建立索引 bool Buildindex(const std::string &input) { std::ifstream in(input,std::ios::in | std::ios::binary); if(!in.is_open()) { std::cerr<<"Buildindex is errno"<<std::endl; return false; } //存单个读取文档内容 std::string line; while(std::getline(in,line)) { //正排 Forwardindex * doc = BUild_Forward_Index(line); printf("正在建立索引:%lld\ntitle:%s\nchain:%s\n",doc->doc_id,doc->title.c_str(),doc->chain.c_str()); if(doc==nullptr)continue; //倒排 Build_Inverted_Index(*doc); } return true; }
(5)建立正排索引

思路:首先我们拿到了单个文档的内容,也就是 line(string)变量,然后对这个内容进行解析,提取出标题、内容、URL(每个line变量都是:标题 | 内容 | URL 的格式,分割即可!)然后填入到vector里面,该文档的ID刚好就是vector的下标,下面有正排的结构体,大家可以对照着看:

//建立正排索引 Forwardindex *BUild_Forward_Index(const std::string&line) { Forwardindex* index = new Forwardindex(); //截取title size_t set_pos1 = line.find('\3'); if(set_pos1 == std::string::npos) { delete index; return nullptr; } index->title = line.substr(0, set_pos1); if (index->title.empty()) index->title = "空"; //截取source size_t set_pos2 = line.find('\3', set_pos1+1); if(set_pos2 == std::string::npos) { delete index; return nullptr; } index->source = line.substr(set_pos1+1, set_pos2 - (set_pos1+1)); if (index->source.empty()) index->source = "空"; //截取URL(chain) index->chain = line.substr(set_pos2+1); if (index->chain.empty()) index->chain = "空"; index->doc_id = Forward.size(); Forward.push_back(*index); return index; }
//正排结构 typedef struct Forward_index { //文档内容 std::string title; std::string source; std::string chain; //对应正排ID uint64_t doc_id; }Forwardindex;
(6)建立倒排索引
//建立倒排索引 bool Build_Inverted_Index(const Forwardindex& doc) { //建立分词对象 JiebaUtil jieba; std::vector<std::string> S; //对标题统计 S=jieba.Tokenize(doc.title); //此时V存放的是每个标题的分词,然后统计结果和出现次数 struct culculate { int title_size=0; int source_size=0; }; std::unordered_map<std::string,culculate> V; //遍历S统计标题的出现次数 for(auto e:S) { (V[e].title_size)++; } //统计内容出现次数 S.clear(); S=jieba.Tokenize(doc.source); for(auto e:S) { (V[e].source_size)++; } //遍历V的结果写入到Inverted for(auto it:V) { Invertedindex index_t; index_t.word=it.first; index_t.doc_id=doc.doc_id; index_t.weight=((it.second.title_size)*2+(it.second.source_size)*1); Inverted[it.first].push_back(std::move(index_t)); } return true; }

【四】单例模式

单例模式:只允许创建一个Index对象,后面只能由 index 指针调用该对象

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; class Index { public: typedef std::vector<Invertedindex> Stock_Inverted; //禁止拷贝构造和赋值重载 Index(const Index&)=delete; Index& operator=(const Index&)=delete; static Index*handle() { if(instance==nullptr) { pthread_mutex_lock(&mutex); if (instance == nullptr) { instance = new Index; } pthread_mutex_unlock(&mutex); } return instance; } ...... }
 //静态成员初始化 Index* Index::instance=nullptr;

Read more

PostgreSQL:简介与安装部署

PostgreSQL:简介与安装部署

🧑 博主简介:ZEEKLOG博客专家,历代文学网(PC端可以访问:https://literature.sinhy.com/#/?__c=1000,移动端可微信小程序搜索“历代文学”)总架构师,15年工作经验,精通Java编程,高并发设计,Springboot和微服务,熟悉Linux,ESXI虚拟化以及云原生Docker和K8s,热衷于探索科技的边界,并将理论知识转化为实际应用。保持对新技术的好奇心,乐于分享所学,希望通过我的实践经历和见解,启发他人的创新思维。在这里,我希望能与志同道合的朋友交流探讨,共同进步,一起在技术的世界里不断学习成长。 技术合作请加本人wx(注明来自ZEEKLOG):foreast_sea

By Ne0inhk
FastChat 架构拆解:打造类 ChatGPT 私有化部署解决方案的基石

FastChat 架构拆解:打造类 ChatGPT 私有化部署解决方案的基石

🐇明明跟你说过:个人主页 🏅个人专栏:《深度探秘:AI界的007》 🏅 🔖行路有良友,便是天堂🔖 目录 一、FastChat 介绍 1、大语言模型本地部署的需求 2、FastChat 是什么 3、FastChat 项目简介 二、FastChat 系统架构详解 1、controller 2、model_worker 3、openai_api_server 4、web UI 前端 一、FastChat 介绍 1、大语言模型本地部署的需求 为什么明明有 ChatGPT、Claude 这些在线服务可用,大家还要花大力气去做 大语言模型本地部署 呢?🤔 其实就像吃饭一样,有人喜欢外卖(云服务)

By Ne0inhk

Windows/Linux双平台保姆教程:用DDNS-GO v6.7.6实现免费内网穿透(替代花生壳)

从零构建你的专属动态域名服务:告别付费内网穿透,拥抱开源DDNS-GO 最近和几个独立开发者朋友聊天,大家普遍吐槽的一个点就是内网穿透服务。无论是为了远程调试家里的NAS,还是想临时给客户演示一个部署在本地开发机的Web应用,传统的方案要么像花生壳这类工具需要付费且流量受限,要么配置复杂得让人望而却步。更别提一些云服务商提供的穿透服务,按流量计费的模式对于高频测试来说,成本完全不可控。其实,如果你手头有一个公网IP(哪怕是动态变化的),或者你的IPv6环境是通畅的,完全没必要依赖第三方付费服务。今天,我们就来深入聊聊如何利用一个名为 DDNS-GO 的开源神器,亲手搭建一套稳定、免费且完全自控的动态域名解析系统,彻底摆脱对商业内网穿透工具的依赖。 DDNS-GO 的核心价值在于它的“桥梁”作用。它持续监测你本地网络的公网IP地址(包括IPv4和IPv6),一旦发现IP发生变化,就立刻调用云解析服务商(如阿里云、腾讯云DNSPod、Cloudflare等)的API,自动将你指定的域名更新解析到新的IP上。这样一来,无论你的网络环境如何变动,通过一个固定的域名,你总能从外网访问到家里的

By Ne0inhk