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高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...
随机推荐
- Node js redis
const redis = require('redis');//redis , '172.16.4.2'); //redis操作 client.set("hello", &quo ...
- RabbitMQ 实现远程过程调用RPC
RPC调用的顺序1. 在客户端初始化的时候,也就是SimpleRpcClient类初始化的时候,它会随机的创建一个callback队列,用于存放服务的返回值,这个队列是exclusive的.连接断开就 ...
- CentOS7使用yum安装配置Redis
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- Gradle 下载的依赖包在什么位置?
Mac系统默认下载到:/Users/(用户名)/.gradle/caches/modules-2/files-2.1Windows系统默认下载到:C:\Users\(用户名)\.gradle\cach ...
- Android进阶:一、日志打印和保存策略
前言: 项目开始没有做好日志统计工作,每次有问题后端都得找前端对接,严重影响工作效率.最近特地在项目中加上日志保存策略,在此分享,供需要的人学习. 一.更详细的日志信息 既然决定自定义一个log,那我 ...
- linux 端口占用
进程id为9106,进程名称为java的进程,占用了8080端口(监听了8080端口)
- Picnic Planning POJ - 1639(最小k度生成树)
The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible abil ...
- datatable中的copy和clone的用法区分
dt.copy();//复制结构和数据 dt.clone();//仅复制结构,不复制数据
- websocket与ajax的区别浅析
1.本质不同 Ajax,即异步JavaScript和XML,是一种创建交互式网页应用的网页开发技术: WebSocket是HTML5一种新的协议,实现了浏览器与服务器全双工通信.其本质是先通过HT ...
- php 记录日志时 基础的日志格式
2019-02-19 11:29:56 /api/shop/shopManagements?shopId=undefined REQUEST:HTTP://mutest.drcloud.cn/mo ...