Xapian使用入門
關鍵字:搜尋引擎、Xapian
一篇拖了兩三年的入門總結文章,今天發出來,一方面是自己的總結,另一方面是給自己和他人的備忘。讀者需要對搜尋引擎有初步瞭解,譬如瞭解倒排、term、doc、相似度打分等概念。
Xapian是一個C++搜尋引擎核心,提供了類似Lucene的功能,功能沒有Lucene豐富,但可以滿足常見的搜尋需求:倒排檢索、與或非查詢、相關性打分排序等。
下面對Xapian入門使用做介紹。
1、Xapian安裝
下面介紹Linux下的編譯安裝。
(1)下載原始碼
從https://oligarchy.co.uk/xapian (推薦)或者https://github.com/xapian/xapian 下載xapian-core包。從github下載的包不包括./configure檔案,需要自己使用autoconf生成。xapian-core不同版本對GCC有不同要求,可以看github下的INSTALL檔案。
(2)解壓&安裝
解壓:xz -d xx.tar.xz tar xvf xx.tar
配置:./configure --prefix=[your install path]
編譯安裝:make –j2 && make install
注:valgrind 檢查可能會執行很久,修改./configure 禁止掉它:在使用VALGRIND之前增加一行:VALGRIND=
2、Hello World
demo例子:對“中國籃球比賽”建索引,然後使用OR語法做檢索,輸出檢索結果資訊。
程式碼:
/*************************************************************************** * * @file hello_world.cpp * @author cswuyg * @date 2019/01 * @briefxapian first demo * **************************************************************************/ #include <iostream> #include "xapian.h" const char* const K_DB_PATH = "index_data"; const char* const K_DOC_UNIQUE_ID = "007"; /// 建立索引 void createIndex() { std::cout << "--index start--" << std::endl; Xapian::WritableDatabase db(K_DB_PATH, Xapian::DB_CREATE_OR_OPEN); Xapian::Document doc; doc.add_posting("T中國", 1); doc.add_posting("T籃球", 1); doc.add_posting("T比賽", 1); 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; } /// 檢索索引 void queryIndex() { 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::Database db(K_DB_PATH); Xapian::Enquire enquire(db); enquire.set_query(query); Xapian::MSet result = enquire.get_mset(0, 10); 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() { createIndex(); queryIndex(); return 0; }
makefile:
OBJ=hello_world.o CC=g++ -std=c++11 CFLAGS= XAPIANROOTDIR=/usr/local/app/cswuyg/xapian_proj/install/ INCLUDE=-I$(XAPIANROOTDIR)include/ LIBS=-lxapian hello_world: $(OBJ) $(CC) $(CFLAGS) -o hello_world $(OBJ)-L$(XAPIANROOTDIR)lib $(LIBS) hello_world.o: hello_world.cpp $(CC) $(CFLAGS) -c hello_world.cpp $(INCLUDE) clean: rm *.o
執行結果:
--index start-- add doc innerId=1 --index finish-- --search start-- query=Xapian::Query((T中國 OR T比賽 OR T足球)) find results count=1 doc=中國籃球比賽,weight=0.308301,percent=66 --search finish--
本文所在:https://www.cnblogs.com/cswuyg/p/10402218.html