gpt4 book ai didi

c++ - 带空格的 Xapian 过滤

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:23 25 4
gpt4 key购买 nike

如何过滤带有空格的查询的搜索结果。

#include <iostream>
#include <string>
#include <xapian.h>
struct document{
std::string title;
std::string content;
std::string url;
};

void indexData(document d) {
try {
Xapian::WritableDatabase db("/Users/ramesh/Desktop/xapian", Xapian::DB_CREATE_OR_OPEN);
Xapian::TermGenerator indexer;
Xapian::Stem stemmer("english");
indexer.set_stemmer(stemmer);
Xapian::Document doc;
doc.set_data(d.title);
indexer.set_document(doc);
indexer.index_text(d.title,1,"title");
indexer.index_text(d.content,1,"content");
indexer.index_text(d.url,1,"url");
doc.add_boolean_term("title"+d.title);
db.replace_document(d.url,doc);
db.commit();
} catch (const Xapian::Error &e) {
std::cout << e.get_description() << std::endl;
exit(1);
}
}

void searchData(std::string query_string){
try{
Xapian::Database db("/Users/ramesh/Desktop/xapian");
Xapian::Enquire enquire(db);
Xapian::QueryParser qp;
Xapian::Stem stemmer("english");
qp.set_default_op(Xapian::Query::OP_FILTER);
qp.set_stemmer(stemmer);
qp.add_prefix("","title");
qp.add_prefix("","content");
qp.add_boolean_prefix("title","title");
qp.set_database(db);
qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
Xapian::Query query = qp.parse_query(query_string);
std::cout << "Parsed query is: " << query.get_description() << std::endl;
enquire.set_query(query);
Xapian::MSet matches = enquire.get_mset(0, 10);
std::cout << matches.get_matches_estimated() << " results found.\n";
std::cout << "Matches 1-" << matches.size() << ":\n" << std::endl;
for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
std::cout << i.get_rank() + 1 << ": " << i.get_weight() << " docid=" << *i
<< " [" << i.get_document().get_data() << "]\n\n";
}
} catch (const Xapian::Error &e) {
std::cout << e.get_description() << std::endl;
exit(1);
}

}

int main()
{
document d1,d2;
d1.title = "Xapain is good";
d1.content = "Xapian is an open source search engine library, which allows developers to add advanced indexing and search facilities to their own applications.";
d1.url = "http://www.xapian.org";
d2.title = "Xapain is awesome";
d2.content = "good Xapian is an open source search engine library, which allows developers to add advanced indexing and search facilities to their own applications.";
d2.url = "http://www.xapian.org/test";
indexData(d1);
indexData(d2);
searchData("xapian title:good");
searchData("xapian title:Xapian is good");
return 0;
}

第一个查询“xapian title:good”运行良好。

但是“xapian title:Xapian is good”失败了。

谁能解释一下这是什么问题

最佳答案

您将标题定义为:

d1.title = "Xapain 不错";

您的搜索查询是:

searchData("xapian title:Xapian is good");

问题可能是您刚刚将“Xapian”拼错为“Xapain”吗?

关于c++ - 带空格的 Xapian 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18932682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com