BoostCompass(建立正排索引和倒排索引模块)

这篇具有很好参考价值的文章主要介绍了BoostCompass(建立正排索引和倒排索引模块)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

BoostCompass(建立正排索引和倒排索引模块),BoostCompass搜索引擎项目,搜索引擎,c++,数据结构

一、模块概述

这个模块我们定义了一个名为Index的C++类,用于构建和维护一个文档索引系统。该系统采用单例模式确保只有一个索引实例,并使用正排索引和倒排索引来快速检索文档。正排索引存储了文档的基本信息,如标题、内容和URL,而倒排索引则根据关键词将文档分组。类中提供了构建索引、获取文档信息和获取倒排列表的方法。构建索引的过程涉及读取处理过的数据文件,解析文档数据,并根据文档内容构建索引。此外,我们还实现了简单的进度显示功能。整个索引系统的构建旨在提高文档检索的效率和准确性。

二、编写正排索引和倒排索引模块

✅安装 jsoncpp

🔴安装方法:sudo yum install -y jsoncpp-devel

✅Jieba分词库的安装

PS:我们要先在Linux机器上安装Jieba分词库链接:🔴 "结巴(Jieba)"中文分词的C++版本

BoostCompass(建立正排索引和倒排索引模块),BoostCompass搜索引擎项目,搜索引擎,c++,数据结构

1. 代码基本框架

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <mutex>
#include "util.hpp" 
#include "log.hpp"  

namespace ns_index {
    // 定义文档信息结构体
    struct DocInfo {
        std::string title;   // 文档的标题
        std::string content; // 文档内容(去标签后)
        std::string url;     // 文档的URL
        uint64_t doc_id;     // 文档的唯一ID
    };

    // 定义倒排列表中的元素结构体
    struct InvertedElem {
        uint64_t doc_id;   // 文档ID
        std::string word;  // 关键字
        int weight;        // 关键字权重
        InvertedElem() : weight(0) {} // 默认构造函数,权重初始化为0
    };
	
	// 获取单例模式的实例
    static Index* GetInstance() {
    	// 双重检查锁定模式,确保线程安全地获取单例
        if (nullptr == instance) {
            mtx.lock();
            if (nullptr == instance) {
                instance = new Index();
            }
            mtx.unlock();
        }
        return instance;
    }
    
    // 定义索引类Index
    class Index {
    private:
        // 构造函数、拷贝构造函数和赋值操作符都设置为私有,防止被实例化
        Index() {}
        Index(const Index&) = delete;
        Index& operator=(const Index&) = delete;

        // 单例模式的实例指针
        static Index* instance;
        // 保护单例模式的互斥锁
        static std::mutex mtx;

    public:
        // 析构函数
        ~Index() {}
        // 根据关键字获取倒排拉链
        InvertedList* GetInvertedList(const std::string& word) {
            auto iter = inverted_index.find(word);
            if (iter == inverted_index.end()) {
                std::cerr << word << " have no InvertedList" << std::endl;
                return nullptr;
            }
            return &(iter->second);
        }
    };
    // 初始化单例模式的实例指针为nullptr
    Index* Index::instance = nullptr;
    // 初始化互斥锁
    std::mutex Index::mtx;
}

代码分析

  1. 文档信息结构体 (DocInfo):

    • 定义了存储文档信息的结构体,包括标题、内容、URL和文档ID。
  2. 倒排列表元素结构体 (InvertedElem):

    • 定义了倒排列表中的元素结构体,包括文档ID、关键字和关键字权重。
  3. 单例模式的实现 (Index 类):

    • Index 类使用单例模式来确保整个程序中只有一个索引实例。
    • 构造函数、拷贝构造函数和赋值操作符都是私有的,防止外部直接创建实例。
    • GetInstance 方法用于获取索引实例,采用双重检查锁定模式来确保线程安全。
    • GetInvertedList 方法用于根据关键字获取对应的倒排列表。
  4. 全局变量和互斥锁

    • instance 是一个静态指针,指向Index类的实例。
    • mtx 是一个静态互斥锁,用于保护单例模式的实例创建过程。

总体来说,上面的代码展示了一个索引系统的基础框架,包括文档信息的存储结构和单例模式的索引管理。

2. 正排索引的建立

// 定义宏常量
#define NUM 101

// 正排索引存储文档信息
std::vector<DocInfo> forward_index;

// 根据文档ID获取文档信息
DocInfo* GetForwardIndex(uint64_t doc_id) {
    if (doc_id >= forward_index.size()) {
        std::cerr << "doc_id out of range, error!" << std::endl;
        return nullptr;
    }
    return &forward_index[doc_id];
}

// 构建索引,输入为处理完毕的数据文件路径
bool BuildIndex(const std::string& input) {
    // 打开输入文件
    std::ifstream in(input, std::ios::in | std::ios::binary);
    if (!in.is_open()) {
        std::cerr << "sorry, " << input << " open error" << std::endl;
        return false;
    }

    // 读取文件行并构建索引
    std::string line;
    int count = 0;
    std::string bar(NUM, ' '); // 创建进度条
    bar[1] = '=';
    while (std::getline(in, line)) {
        DocInfo* doc = BuildForwardIndex(line);
        if (nullptr == doc) {
            continue;
        }

        BuildInvertedIndex(*doc);
        count++;

        // 显示进度
        if (count % 86 == 0) {
            int cnt = count / 86 + 1;
            bar[cnt] = '=';
            std::cout << "成功建立索引进度: " << bar << " [" << cnt << "%]" << "\r";
            std::cout.flush();
        }
    }
    std::cout << std::endl;
    return true;
}

// 私有辅助函数,用于构建正排索引
DocInfo* BuildForwardIndex(const std::string& line) {
    // 分割字符串为标题、内容和URL
    std::vector<std::string> results;
    const std::string sep = "\3"; // 行内分隔符
    ns_util::StringUtil::Split(line, &results, sep);
    if (results.size() != 3) {
        return nullptr;
    }

    // 创建文档信息并添加到正排索引
    DocInfo doc;
    doc.title = results[0];
    doc.content = results[1];
    doc.url = results[2];
    doc.doc_id = forward_index.size();
    // 插入到正排索引的vector
    forward_index.push_back(std::move(doc));
    return &forward_index.back();
}

代码分析

  1. forward_index 是一个 std::vector,用于存储所有文档的正排索引信息。
  2. GetForwardIndex 函数通过文档ID从正排索引中检索文档信息。如果文档ID超出范围,则返回空指针并打印错误信息。
  3. BuildIndex 函数用于从数据文件中读取文档数据并构建索引。它打开输入文件,逐行读取并处理每一行,构建正排索引和倒排索引,并显示进度条。
  4. BuildForwardIndex 函数是一个私有辅助函数,用于构建单个文档的正排索引条目。它将输入行分割为标题、内容和URL,创建一个 DocInfo 对象,并将其添加到 forward_index 向量中。

3. 倒排索引的建立

// 定义宏常量
#define X 10
#define Y 1

// 倒排索引存储关键字到倒排列表的映射
std::unordered_map<std::string, InvertedList> inverted_index;

// 定义倒排列表的类型为InvertedElem元素的向量
typedef std::vector<InvertedElem> InvertedList;

// 私有辅助函数,用于构建倒排索引
bool BuildInvertedIndex(const DocInfo& doc) {
    // 分词并统计词频
    struct word_cnt {
        int title_cnt;
        int content_cnt;

        word_cnt() : title_cnt(0), content_cnt(0) {}
    };

    // 用来暂存词频的映射表
    std::unordered_map<std::string, word_cnt> word_map;

    // 对标题进行分词
    std::vector<std::string> title_words;
    ns_util::JiebaUtil::CutString(doc.title, &title_words);

    // 对标题进行词频统计
    for (std::string s : title_words) {
        boost::to_lower(s);  // 将单词转换为小写
        word_map[s].title_cnt++;  // 如果存在就增加计数,否则创建新条目
    }

    // 对文档内容进行分词
    std::vector<std::string> content_words;
    ns_util::JiebaUtil::CutString(doc.content, &content_words);

    // 对内容进行词频统计
    for (std::string s : content_words) {
        boost::to_lower(s);
        word_map[s].content_cnt++;
    }

    // 构建倒排列表
    for (const auto& word_pair : word_map) {
        InvertedElem item;
        item.doc_id = doc.doc_id;
        item.word = word_pair.first;
        // 计算权重,标题中的词乘以X,内容中的词乘以Y
        item.weight = X * word_pair.second.title_cnt + Y * word_pair.second.content_cnt;
        // 获取对应关键字的倒排列表,并添加新的倒排元素
        InvertedList& inverted_list = inverted_index[word_pair.first];
        inverted_list.push_back(std::move(item));
    }

    return true;
}

代码分析

  1. 定义数据结构

    • DocInfo 结构体定义了文档信息,包括标题、内容、URL和唯一的文档ID。
    • InvertedElem 结构体定义了倒排列表中的元素,包括文档ID、关键字和权重。
    • InvertedList 类型定义为 std::vector<InvertedElem>,表示一个倒排列表,包含多个 InvertedElem 元素。
  2. 构建正排索引

    • forward_index 是一个 std::vector<DocInfo>,用于存储所有文档的正排索引信息。
    • GetForwardIndex 函数通过文档ID从正排索引中检索文档信息。
  3. 构建倒排索引

    • inverted_index 是一个 std::unordered_map<std::string, InvertedList>,用于存储关键字到倒排列表的映射。
    • BuildInvertedIndex 函数用于根据文档信息构建倒排索引。它首先对文档的标题和内容进行分词,然后统计每个词在标题和内容中出现的次数(词频)。
    • 每个分词后的词都会被转换为小写,以便进行不区分大小写的搜索。
    • 为每个词创建一个 InvertedElem 对象,并根据其在标题和内容中的出现次数计算权重。
    • InvertedElem 对象添加到 inverted_index 中对应关键字的倒排列表中。
  4. 处理文本数据

    • BuildIndex 函数打开并读取输入文件,该文件包含处理完毕的文档数据。
    • 对文件中的每一行数据,使用 BuildForwardIndex 函数构建正排索引条目,并调用 BuildInvertedIndex 函数构建倒排索引。
    • 在构建索引的过程中,显示进度条以指示索引构建的进度。

整体来说,上面这段代码展示了如何从文本数据中提取文档信息,并构建正排索引和倒排索引,以便在搜索引擎中快速检索相关文档。通过倒排索引,可以有效地根据关键字找到所有相关文档,提高搜索效率。文章来源地址https://www.toymoban.com/news/detail-850250.html

三、整体代码

⭕index.hpp

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>
#include <mutex>
#include "util.hpp" 
#include "log.hpp"  

#define NUM 101
#define X 10
#define Y 1

namespace ns_index {
    // 定义文档信息结构体
    struct DocInfo {
        std::string title;   // 文档的标题
        std::string content; // 文档内容(去标签后)
        std::string url;     // 文档的URL
        uint64_t doc_id;     // 文档的唯一ID
    };

    // 定义倒排列表中的元素结构体
    struct InvertedElem {
        uint64_t doc_id;   // 文档ID
        std::string word;  // 关键字
        int weight;        // 关键字权重
        InvertedElem() : weight(0) {} // 默认构造函数,权重初始化为0
    };

    // 倒排拉链储存列表
    typedef std::vector<InvertedElem> InvertedList;

    // 定义索引类Index
    class Index {
    private:
        // 正排索引存储文档信息
        std::vector<DocInfo> forward_index;
        // 倒排索引存储关键字到倒排列表的映射
        std::unordered_map<std::string, InvertedList> inverted_index;

        // 构造函数、拷贝构造函数和赋值操作符都设置为私有,防止被实例化
        Index() {}
        Index(const Index&) = delete;
        Index& operator=(const Index&) = delete;

        // 单例模式的实例指针
        static Index* instance;
        // 保护单例模式的互斥锁
        static std::mutex mtx;

    public:
        // 析构函数
        ~Index() {}
        // 获取单例模式的实例
        static Index* GetInstance() {
            // 双重检查锁定模式,确保线程安全地获取单例
            if (nullptr == instance) {
                mtx.lock();
                if (nullptr == instance) {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }

        // 根据文档ID获取文档信息
        DocInfo* GetForwardIndex(uint64_t doc_id) {
            if (doc_id >= forward_index.size()) {
                std::cerr << "doc_id out of range, error!" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }

        // 根据关键字获取倒排拉链
        InvertedList* GetInvertedList(const std::string& word) {
            auto iter = inverted_index.find(word);
            if (iter == inverted_index.end()) {
                std::cerr << word << " have no InvertedList" << std::endl;
                return nullptr;
            }
            return &(iter->second);
        }

        // 构建索引,输入为处理完毕的数据文件路径
        bool BuildIndex(const std::string& input) {
            // 打开输入文件
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open()) {
                std::cerr << "sorry, " << input << " open error" << std::endl;
                return false;
            }

            // 读取文件行并构建索引
            std::string line;
            int count = 0;
            std::string bar(NUM, ' '); // 创建进度条
            bar[1] = '=';
            while (std::getline(in, line)) {
                DocInfo* doc = BuildForwardIndex(line);
                if (nullptr == doc) {
                    continue;
                }

                BuildInvertedIndex(*doc);
                count++;

                // 显示进度
                if (count % 86 == 0) {
                    int cnt = count / 86 + 1;
                    bar[cnt] = '=';
                    std::cout << "成功建立索引进度: " << bar << " [" << cnt << "%]" << "\r";
                    std::cout.flush();
                }
            }
            std::cout << std::endl;
            return true;
        }
    private:
        // 私有辅助函数,用于构建正排索引
        DocInfo* BuildForwardIndex(const std::string& line) {
            // 分割字符串为标题、内容和URL
            std::vector<std::string> results;
            const std::string sep = "\3"; // 行内分隔符
            ns_util::StringUtil::Split(line, &results, sep);
            if (results.size() != 3) {
                return nullptr;
            }

            // 创建文档信息并添加到正排索引
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size();
            //插入到正排索引的vector
            forward_index.push_back(std::move(doc));
            return &forward_index.back();
        }

        // 私有辅助函数,用于构建倒排索引
        bool BuildInvertedIndex(const DocInfo& doc) {
            // 分词并统计词频
            struct word_cnt{
                    int title_cnt;
                    int content_cnt;

                    word_cnt():title_cnt(0), content_cnt(0){}
            };
        
            std::unordered_map<std::string, word_cnt> word_map; //用来暂存词频的映射表

            //对标题进行分词
            std::vector<std::string> title_words;
            ns_util::JiebaUtil::CutString(doc.title, &title_words);

            //对标题进行词频统计
            for(std::string s : title_words){
                boost::to_lower(s);      //需要统一转化成为小写
                word_map[s].title_cnt++; //如果存在就获取,如果不存在就新建
            }

            //对文档内容进行分词
            std::vector<std::string> content_words;
            ns_util::JiebaUtil::CutString(doc.content, &content_words);
                
            //对内容进行词频统计
            for(std::string s : content_words){
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }
            // 构建倒排列表
            for (const auto& word_pair : word_map) {
                InvertedElem item;
                item.doc_id = doc.doc_id;
                item.word = word_pair.first;
                item.weight = X * title_cnt.title_cnt + Y * content_cnt.content_cnt;
                InvertedList& inverted_list = inverted_index[word_pair.first];
                inverted_list.push_back(std::move(item));
            }

            return true;
        }
    };
    // 初始化单例模式的实例指针为nullptr
    Index* Index::instance = nullptr;
    // 初始化互斥锁
    std::mutex Index::mtx;
}

到了这里,关于BoostCompass(建立正排索引和倒排索引模块)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Golang系统开发】搜索引擎(3) 压缩倒排索引表

    假设我们的数据集中有 800000 篇文章,每篇文章有 200 词条,每个词条有6个字符,倒排记录数目是 1 亿。那么如果我们倒排索引表中单单记录文档id,不记录文档内的频率和偏移信息。 那么 文档id 的长度就必须是 l o g 2 800000 = 20 b i t log_2800000=20 bit l o g 2 ​ 800000 = 20 bi t (文档

    2024年02月12日
    浏览(32)
  • 搜索引擎:常用信息检索方式介绍与倒排索引实现(Python)

    (1)线性扫描 计算机对于文档内容检索有多种可能的方式,如直接从头遍历至尾端,根据我们输入的提取内容。 这类检索方式与我们人类阅读的习惯相同,因此实现简单且很容易被接受。 若问你《三国演义》中是否存在’舌战群儒’这一词语,我们常常会选择浏览全文

    2024年02月08日
    浏览(29)
  • 深入了解Elasticsearch搜索引擎篇:倒排索引、架构设计与优化策略

    倒排索引是一种用于快速检索的数据结构,常用于搜索引擎和数据库中。与传统的正排索引不同,倒排索引是根据来建立索引,而不是根据文档ID。 倒排索引的建立过程如下:首先,将每个文档拆分成一系列的或词项,然后建立一个词项到文档的映射。对每个关

    2024年02月12日
    浏览(38)
  • Python实战:在搜索引擎开发中的倒排索引与检索算法

    在信息检索领域,搜索引擎是一个至关重要的工具,它可以帮助用户在大量的数据中找到所需的信息。而倒排索引是搜索引擎的核心技术之一,它能够提高检索的效率。 倒排索引是一种数据结构,它将文档的内容和文档的ID关联起来。在倒排索引中,每个词项都有一个列表,

    2024年04月26日
    浏览(25)
  • ES入门十一:正排索引和倒排索引

    索引本质上就是一种加快检索数据的存储结构,就像书本的目录一下。 为了更好的理解正排索引和倒排索引,我们借由一个 **唐诗宋词比赛,**这个比赛一共有两个项目: 给定诗词名称,背诵整首 给诗词中几个词语,让你说出带这些词语的诗词。 不难想到,1比较简单,就是

    2024年04月10日
    浏览(63)
  • 【Elasticsearch专栏 02】深入探索:Elasticsearch为什么使用倒排索引而不是正排索引

    Elasticsearch选择使用倒排索引而不是正排索引,主要是基于倒排索引在处理全文搜索和大规模数据集时的优势。下面将详细解释为什么Elasticsearch更倾向于使用倒排索引,并提供一些简化的代码片段来说明这两种索引结构的基本差异。 正排索引是一种将文档映射到其包含的单词

    2024年02月22日
    浏览(35)
  • ES高频面试问题:一张图带你读懂 Elasticsearch 中“正排索引(正向索引)”和“倒排索引(反向索引)”区别

    从广义来说,doc values 本质上是一个序列化的 列式存储 。列式存储 适用于聚合、排序、脚本等操作,所有的数字、地理坐标、日期、IP 和不分词( not_analyzed )字符类型都会默认开启, 不支持 text 和 annotated_text 类型 倒排 :即 词项 = 包含当前词项的doc_id的列表 的映射。倒排

    2024年02月02日
    浏览(40)
  • 相似性搜索:第 3 部分--混合倒排文件索引和产品量化

    接续前文:相似性搜索:第 2 部分:产品量化  S Imilarity 搜索 是一个问题,给定一个查询的目标是在所有数据库文档中找到与其最相似的文档。         在数据科学中,相似性搜索经常出现在NLP领域,搜索引擎或推荐系统中,其中需要检索最相关的文档或项目以进行查询

    2024年02月07日
    浏览(31)
  • 简述Elasticsearch(ES)是什么 全文搜索概念 (倒排索引 管理文档)

    今天 我们来说说 NoSql 中的 Elasticsearch 大家基本都叫它 ES 官方介绍 它是一个分布式全文搜索引擎 分布式是一个系统架构的概念 而 全文搜索引擎 全文搜索 可以说基本大家天天都在接触 就比如 我们京东购物 想买什么东西 在全文输入框中搜索 它就会在所有物品中 帮你找出需

    2024年01月25日
    浏览(38)
  • 正排倒排,并不是 MySQL 的排序的全部!

    一个悠闲的上午,小航送了我,一袋坚果,他看我吃的正香,慢慢问道:”温哥,mysql的排序,有什么要注意的吗,不就是正排倒排吗?” 我一听他问我的问题,顿感坚果不香了,但是为了技术(mainzi),我装作大师的说道: “正排倒排,当然不是全部,你最少要知道,2个

    2024年02月09日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包