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高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里, 不用一页一页 ...
随机推荐
- java中读取资源文件的方法
展开全部 1.使用java.util.Properties类的load()方法 示例: //文件在项目下.不是在包下!! InputStream in = new BufferedInputStrea ...
- FSBPM 开发过程中一些提醒备注信息(供参考)
------智能OA系统开发过程中 前端开发前端 搜索查询的配置 运算操作符: like equals 共两种筛选数据方式. html标签上配置一下eg: <inpu ...
- Ubuntu 下使用 Nginx 部署 .NET Core 2.0 网站
前言 本文介绍如何在 Ubuntu 16.04 服务器上安装 .NET Core 2.0 SDK.创建项目与发布,并使用 Nginx 部署 .NET Core 2.0 Web 项目. 安装 .NET ...
- react-native项目中禁止截屏与录屏
在android/app/src/main/java/com/projname/MainActivity.java文件中的onCreate方法添加一下代码即可 import android.view. ...
- 打包ideaUI本地项目,以供本地使用
#首先我们要在本机进行一些配置 在本机配置环境变量(控制面板->高级系统设置->环境变量->) #用cmd检测是否配置成功 如果你在ideaUI里,配置好了之后.我们现在来打架包 # ...
- python基础day3
一.文件管理 文件管理是很多应用程序的基本功能和重要组成部分.Python可以使文件管理极其简单,特别是和其它语言相对比. 1. 读操作 1.1r模式 1.1.1读取其他路径下文件 首先在D盘创 ...
- SpringBoot主程序注解@SpringBootApplication简单分析
一.@SpringBootApplication说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用: @SpringBootA ...
- python: 列表的方法
操作 函数 使用方法 备注 索引 index in: example.index(‘creative’) --- 1 in:example[1,] --- [’creative’, [’京东’,996 ...
- java实现点击图片文字验证码
https://www.cnblogs.com/shihaiming/p/7657115.html
- 【转】JY 博客
http://www.lovewebgames.com/demo.html http://www.lovewebgames.com/