关键字:xapian、内存索引

  xapian除了提供用于生产环境的磁盘索引,也提供了内存索引(InMemoryDatabase)。内存索引。我们可以通过观察内存索引的设计,来了解xapian的设计思路。

1 用途

  官方文档说法:

  “inmemory, This type is a database held entirely in memory. It was originally written for testing purposes only, but may prove useful for building up temporary small databases.”

  早期版本的源码说明:

  “This backend stores a database entirely in memory.  When the database is closed these indexed contents are lost.This is useful for searching through relatively small amounts of data (such as a single large file) which hasn't previously been indexed.”

  早期版本的源码注释:

  “This is a prototype database, mainly used for debugging and testing”

  

  总的来说,这是一个原型DB,最初只用来做测试和debug用,没有持久化,关闭DB数据就丢失,可以用来处理小量数据的搜索,并且这部分数据可以在内存中实时建索引。

2 使用内存索引

/***************************************************************************
*
* @file ram_xapian.cpp
* @author cswuyg
* @date 2019/02/21
* @brief
*
**************************************************************************/
// inmemory index use deprecated class, disalbe the compile error.
#pragma warning(disable : 4996)
#include <iostream>
#include "xapian.h" #pragma comment(lib, "libxapian.a")
#pragma comment(lib, "zdll.lib") const char* const K_DB_PATH = "index_data";
const char* const K_DOC_UNIQUE_ID = ""; Xapian::WritableDatabase createIndex() {
std::cout << "--index start--" << std::endl;
Xapian::WritableDatabase db = Xapian::InMemory::open(); Xapian::Document doc;
doc.add_posting("T世界", );
doc.add_posting("T体育", );
doc.add_posting("T比赛", );
doc.set_data("世界体育比赛");
doc.add_boolean_term(K_DOC_UNIQUE_ID); Xapian::docid innerId = db.replace_document(K_DOC_UNIQUE_ID, doc); std::cout << "add doc innerId=" << innerId << std::endl; db.commit(); std::cout << "--index finish--" << std::endl;
return db;
} void queryIndex(Xapian::WritableDatabase db) {
std::cout << "--search start--" << std::endl;
Xapian::Query termOne = Xapian::Query("T世界");
Xapian::Query termTwo = Xapian::Query("T比赛");
Xapian::Query termThree = Xapian::Query("T体育");
auto query = Xapian::Query(Xapian::Query::OP_OR, Xapian::Query(Xapian::Query::OP_OR, termOne, termTwo), termThree);
std::cout << "query=" << query.get_description() << std::endl; Xapian::Enquire enquire(db);
enquire.set_query(query);
Xapian::MSet result = enquire.get_mset(, );
std::cout << "find results count=" << result.get_matches_estimated() << std::endl; 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(); std::cout << "doc=" << data << ",weight=" << docScoreWeight << ",percent=" << docScorePercent << std::endl;
} std::cout << "--search finish--" << std::endl;
} int main() {
auto db = createIndex();
queryIndex(db);
return ;
}

github: https://github.com/cswuyg/xapian_exercise/tree/master/ram_xapian

3 数据结构

内存索引包含一系列数据结构,通过这些数据结构,可以一窥xapian的索引设计思路。

内存索引数据结构如下图所示:

几个主要的操作类封装:

InMemoryPostList:内存中的postlist,单个term,操作的就是倒排链表;

InMemoryAllDocsPostList:内存中的postlist,整个DB,操作的实际上是termlist表(doc表);

InMemoryTermList: 某个doc的term列表;

InMemoryDatabase: 内存DB;

InMemoryAllTermsList: 内存中的termlist,实际上是整个DB的postlists;

InMemoryDocument:单个doc的操作封装  ;

InMemoryPositionList:内存中的position列表操作封装

Xapian的内存索引的更多相关文章

  1. Xapian的内存索引-添加文档

    本文主要记录Xapian的内存索引在添加文档过程中,做了哪些事情. 内容主要为函数执行过程中的流水线. demo代码: Xapian::WritableDatabase db = Xapian::In ...

  2. lucene 内存索引 和文件索引 合并

    IndexWriter.addIndexes(ramDirectory); http://blog.csdn.net/qq_28042463/article/details/51538283 在luc ...

  3. lucene内存索引库、分词器

    内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1)    因为索引库在内存中,所以访问速度更快. 2)    在程序退出时,索引库中的文件也相应的消失了. 3)    ...

  4. 用xapian来做索引

    最近一个项目需要正则搜索MongoDB,400多万的数据一次查询要20s以上,需要建立一个前端索引服务.本着部署简单.开发容易的原则,找到了xapian这个索引库. 我使用的是Python的接口,xa ...

  5. lucene 内存索引存储每个field里内容的相关代码

    相关的类调用关系 DocumentsWriterPerThread ——>DocFieldProcessor   DocumentsWriterPerThread里的consumer对象(类型是 ...

  6. EasyDarwin开源流媒体服务器进行RTSP转发过程中将sdp由文件存储改成内存索引

    -本篇由团队成员Fantasy供稿! 原始版本 在Darwin Streaming Server版本中,推送端DoAnnounce的时候后服务器会根据easydarwin.xml中配置的movies_ ...

  7. Xapian索引-文档检索过程分析

    本文是Xapian检索过程的分析,本文内容中源码比较多.检索过程,总的来说就是拉取倒排链,取得合法doc,然后做打分排序的过程. 1 理论分析 1.1  检索语法 面对不同的检索业务,我们会有多种检索 ...

  8. Xapian构建索引说明

    Reference: http://www.totogoo.com/article/115/xapian-desc.html Xapian与开源 Xapian的官方网站是http://www.xapi ...

  9. MYSQL索引结构原理、性能分析与优化

    [转]MYSQL索引结构原理.性能分析与优化 第一部分:基础知识 索引 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...

随机推荐

  1. MBR分区操作-增加、扩展、删除

    MBR分区操作-增加.扩展.删除 GPT分区参考 http://www.blogjava.net/haha1903/archive/2011/12/21/366942.html l  fdisk 显示 ...

  2. pycharm创建scrapy项目教程及遇到的坑

    最近学习scrapy爬虫框架,在使用pycharm安装scrapy类库及创建scrapy项目时花费了好长的时间,遇到各种坑,根据网上的各种教程,花费了一晚上的时间,终于成功,其中也踩了一些坑,现在整理 ...

  3. python第十五天

    什么是模块? 一系列功能的集合 定义模块? 创建一个py文件就是一个模块,该py文件名就是模块名 怎么使用模块? 在要是用的模块文件中通过import 模块名 来导入模块 模块的四种方式? 1.编译执 ...

  4. 2018—2019-- 2网络对抗技术20165239Exp信息搜集 漏洞扫描

    一.实验内容 二.实验步骤 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 3.基本的扫描技术 主机发现 端口扫描 OS及服务版本探测 具体服务的查点 4.漏洞扫描 三.实验中遇到的问题 四. ...

  5. event、fly.js、购物车特效

    先总结下区别: #鼠标相对于浏览器窗口可视区域的X,Y坐标(窗口坐标),可视区域不包括工具栏和滚动条. event.clientX.event.clientY #鼠标相对于document文档区域的x ...

  6. Python科学计算库

    Python科学计算库 一.numpy库和matplotlib库的学习 (1)numpy库介绍:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成 ...

  7. 利用jquery-barcode.js实现生成条形码

    jquery-barcode官网 js下载地址-github 代码示范(官网上也有) <!DOCTYPE html> <html> <head> <meta ...

  8. TimesTen数据库表中显示中文乱码的真正原因

    上一篇博客TimesTen中文乱码问题(其实是cmd.exe中文乱码)的内容可能不对,也许只是个巧合?不得而知了.因为我今天重装系统了,把win10换成了win7(64bit).又安装了timeste ...

  9. MyBatis(10)使用association进行分步查询

    (1)接口中编写方法 public Emp getEmpByStep(Integer id); public Dept getDeptById(Integer id); (2)Mapper文件 < ...

  10. Makefile = ?= := 区别 $@,$^,$<

    = 是最基本的赋值:= 是覆盖之前的值?= 是如果没有被赋值过就赋予等号后面的值+= 是添加等号后面的值 1.“=” make会将整个makefile展开后,再决定变量的值.也就是说,变量的值将会是整 ...