我不是李白 2014-02-11 02:56 采纳率: 0%
浏览 1846

Xapian C++中的应用问题

create.cpp创建数据库并写数据
#include // 头文件

#include
#include

#include // For exit().
#include

using namespace std;

#define INDEX_PATH "./index_data"

int main(int argc, char **argv)
{
try
{
// 创建或者打开一个可读写的数据库
Xapian::WritableDatabase db(INDEX_PATH, Xapian::DB_CREATE_OR_OPEN);
// 分词器
Xapian::TermGenerator indexer;
string para;
int flag = 0;
while (flag++ < 8)
{
string line;
if (cin.eof())
{

if (para.empty()) break;
}
else
{
getline(cin, line);
}
if (line.empty())
{
if (!para.empty())
{
// 生成一个文档
Xapian::Document doc;
doc.set_data(para); // 定义文档数据,这些数据对于用户来说是不透明的,用户可以在这里定义文档的一些属性,或URI,路径等信息
// 设置文档,分词
indexer.set_document(doc);
indexer.index_text(para);
// Add the document to the database.
// 把文档加入数据库
db.add_document(doc);
para.resize(0);
}
}
else
{
if (!para.empty()) para += ' ';
para += line;
}
}

    db.flush();
}
catch (const Xapian::Error &e)
{
    cout << e.get_description() << endl;
    exit(1);
}

}

query.cpp查询数据库
#include
#include
#include

#include // For exit().
#include

using namespace std;

#define INDEX_PATH "./index_data"

int main(int argc, char **argv)
{
if (argc != 2)
{
cout<<"Usage:"<<argv[0]<<" QUERY_STRING"<<endl;
exit(0);
}
try
{
//打开数据库
Xapian::Database db(INDEX_PATH);
// Start an enquire session.
// 生成查询会话
Xapian::Enquire enquire(db);
string query_string(argv[1]);
Xapian::QueryParser qp;
qp.set_database(db);
qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
// 解析查询条件
Xapian::Query query = qp.parse_query(query_string);
cout << "Parsed query is: " << query.get_description() << endl;
// 把解析后的查询条件放入查询会话
enquire.set_query(query);
// 得到查询结果
Xapian::MSet matches = enquire.get_mset(0, 10);
// Display the results.
cout << matches.get_matches_estimated() << " results found.\n";
cout << "Matches 1-" << matches.size() << ":\n" << endl;
// 得到查询结果
for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i)
{
cout << i.get_rank() + 1 << ": " << i.get_percent() << "% docid=" << *i
<< " [" << i.get_document().get_data() << "]\n\n";
}
}
catch (const Xapian::Error &e)
{
cout << e.get_description() << endl;
exit(1);
}
return 0;
}

当我编译成功后运行./create
[root@localhost new]# ./create
qwe
qweqw
qweqw
qweqw
qweqw
qweqw
qweqw
qweqw
写入数据完毕
[root@localhost new]# ./query qwe
Parsed query is: Xapian::Query(qwe:(pos=1))
0 results found.
Matches 1-0:
查询的时候就是查询不到 这是为什么呢
一下是我index_data的内容
[root@localhost index_data]# ll
总计 16
-rw-r--r-- 1 root root 0 02-11 10:53 flintlock
-rw-r--r-- 1 root root 28 02-11 09:47 iamchert
-rw-r--r-- 1 root root 13 02-11 09:47 postlist.baseA
-rw-r--r-- 1 root root 0 02-11 09:47 postlist.DB
-rw-r--r-- 1 root root 13 02-11 09:47 record.baseA
-rw-r--r-- 1 root root 0 02-11 09:47 record.DB
-rw-r--r-- 1 root root 13 02-11 09:47 termlist.baseA
-rw-r--r-- 1 root root 0 02-11 09:47 termlist.DB

  • 写回答

1条回答

  • shiter 人工智能领域优质创作者 2015-01-19 07:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题