Xapian的内存索引
关键字: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的内存索引的更多相关文章
- Xapian的内存索引-添加文档
本文主要记录Xapian的内存索引在添加文档过程中,做了哪些事情. 内容主要为函数执行过程中的流水线. demo代码: Xapian::WritableDatabase db = Xapian::In ...
- lucene 内存索引 和文件索引 合并
IndexWriter.addIndexes(ramDirectory); http://blog.csdn.net/qq_28042463/article/details/51538283 在luc ...
- lucene内存索引库、分词器
内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1) 因为索引库在内存中,所以访问速度更快. 2) 在程序退出时,索引库中的文件也相应的消失了. 3) ...
- 用xapian来做索引
最近一个项目需要正则搜索MongoDB,400多万的数据一次查询要20s以上,需要建立一个前端索引服务.本着部署简单.开发容易的原则,找到了xapian这个索引库. 我使用的是Python的接口,xa ...
- lucene 内存索引存储每个field里内容的相关代码
相关的类调用关系 DocumentsWriterPerThread ——>DocFieldProcessor DocumentsWriterPerThread里的consumer对象(类型是 ...
- EasyDarwin开源流媒体服务器进行RTSP转发过程中将sdp由文件存储改成内存索引
-本篇由团队成员Fantasy供稿! 原始版本 在Darwin Streaming Server版本中,推送端DoAnnounce的时候后服务器会根据easydarwin.xml中配置的movies_ ...
- Xapian索引-文档检索过程分析
本文是Xapian检索过程的分析,本文内容中源码比较多.检索过程,总的来说就是拉取倒排链,取得合法doc,然后做打分排序的过程. 1 理论分析 1.1 检索语法 面对不同的检索业务,我们会有多种检索 ...
- Xapian构建索引说明
Reference: http://www.totogoo.com/article/115/xapian-desc.html Xapian与开源 Xapian的官方网站是http://www.xapi ...
- MYSQL索引结构原理、性能分析与优化
[转]MYSQL索引结构原理.性能分析与优化 第一部分:基础知识 索引 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...
随机推荐
- Windows下python2与python3兼容设置
分别安装python2与python3后,我想直接通过命令python2.pip2与python3.pip3区分: 分别进入python安装目录下,修改python.exe为python2.exe.p ...
- datatable拆分多个
/// <summary> /// 分解数据表 /// </summary> /// <param name="originalTab">需要分 ...
- vue v-for循环的用法
1.v-for循环普通数组 ①创建vue对象 ② 循环数据 结果: 2.v-for循环对象数组 ① 创建vue实例对象 ② 循环对象数组 结果: 3.v-for循环对象 ①创建vue对象实例 ②循环对 ...
- ZOJ 2588 Burning Bridges 割边(处理重边)
<题目链接> 题目大意: 给定一个无向图,让你尽可能的删边,但是删边之后,仍然需要保证图的连通性,输出那些不能被删除的边. 解题分析: 就是无向图求桥的题目,主要是提高一下处理重边的姿势. ...
- E - Elevator
E - Elevatorhttp://codeforces.com/gym/241680/problem/E同余最短路,从0~a-1中每一个i向(i+b)%a连一条权值为b的边,向(i+c)%a连一条 ...
- iOS实现应用更新及强制更新
调用更新接口返回字段: result = { descr = ""; isupdate = 1;//是否更新 ...
- 【转载】JAVA基础复习与总结<三> Object类的常用方法
Object类是一个特殊的类,是所有类的父类,如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类.这里主要总结Object类中的三个常用方法:toString().equ ...
- BZOJ5412 : circle
若仅保留这$k$个点仍然有环,那么显然无解. 否则设$A$表示这$k$个点的集合,$B$表示剩下的点的集合,因为是竞赛图,每个集合内部的拓扑关系是一条链,方便起见将所有点按照在所在集合的链上的位置进行 ...
- input type='file' 上传文件 判断图片的大小是否合格与witdh 和 height 是否合格
function CheckFiles(obj) { var array = new Array('gif', 'jpeg', 'png', 'jpg'); //可以上传的文件类型 if (obj.v ...
- 马昕璐 201771010118《面向对象程序设计(java)》第十五周学习总结
第一部分:理论知识学习部分 JAR文件:将.class文件压缩打包为.jar文件后,使用ZIP压缩格式,GUI界面程序就可以直接双击图标运行. 既可以包含类文件,也可以包含诸如图像和声音这些其它类型的 ...