range - Does Xapian support query string like "x: 1..2 OR x: 8..10"? I cannot figure it out -
following simple code, index function establishes indexers , search function searches using querystring directly:
#include <xapian.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <string> using namespace std; int index() { try { xapian::writabledatabase db("./index.db", xapian::db_create_or_open); xapian::termgenerator indexer; xapian::stem stemmer("english"); indexer.set_stemmer(stemmer); (int = 1; < 12; i++) { xapian::document doc; indexer.set_document(doc); string title("title "); string desc("example data number of "); stringstream num; num << i; title.append(num.str()); desc.append(num.str()); indexer.index_text(title, 1, "s"); indexer.index_text(desc, 1, "xd"); indexer.index_text(title); indexer.increase_termpos(); indexer.index_text(desc); doc.set_data(num.str() + "\n" + title + "\n" + desc); doc.add_value(1, num.str()); string idterm = "q" + num.str(); doc.add_boolean_term(idterm); db.add_document(doc); } db.commit(); } catch (const xapian::error &e) { cout << e.get_description() << endl; return 1; } return 0; } int search() { try { xapian::database db("./index.db"); xapian::enquire enquire(db); // xapian::query query(""); xapian::query queryleft(xapian::query::op_value_range, 1, "1", "2"); xapian::query queryright(xapian::query::op_value_range, 1, "8", "11"); xapian::query query(xapian::query::op_or, queryleft, queryright); cout << "parsed query is: " << query.get_description() << endl; enquire.set_query(query); xapian::mset matches = enquire.get_mset(0, 11); cout << matches.get_matches_estimated() << " results found.\n"; cout << "matches 1-" << matches.size() << ":\n" << endl; (xapian::msetiterator = matches.begin(); != matches.end(); ++i) { cout << i.get_rank() + 1 << ":" << i.get_percent() << "%" << endl; cout << i.get_document().get_data() << endl << endl; } } catch (const xapian::error &e) { cout << e.get_description() << endl; return 1; } return 0; } int main() { index(); search(); }
run app, got:
parsed query is: xapian::query((value_range 1 1 2 or value_range 1 8 11)) 8 results found. matches 1-8: 1:100% 1 title 1 example data number of 1 2:100% 2 title 2 example data number of 2 3:100% 10 title 10 example data number of 10 4:100% 11 title 11 example data number of 11 5:100% 1 title 1 example data number of 1 6:100% 2 title 2 example data number of 2 7:100% 10 title 10 example data number of 10 8:100% 11 title 11 example data number of 11
actually want get:
1:100% 1 title 1 example data number of 1 2:100% 2 title 2 example data number of 2 8:100% #8 8 title 8 example data number of 8 9:100% 9 title 9 example data number of 9 10:100% 10 title 10 example data number of 10
did make mistakes? or missed or used wrong query string?
ps: beginner learning xapian.
thank you, @jkalden, @james aylett. got results want. yes, xapian::sortable_serialise() key solve problem.
#include <xapian.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <string> using namespace std; int index() { try { xapian::writabledatabase db("./index.db", xapian::db_create_or_open); xapian::termgenerator indexer; xapian::stem stemmer("english"); indexer.set_stemmer(stemmer); (int = 1; < 12; i++) { xapian::document doc; indexer.set_document(doc); string title("title "); string desc("example data number of "); stringstream num; num << i; title.append(num.str()); desc.append(num.str()); indexer.index_text(title, 1, "s"); indexer.index_text(desc, 1, "xd"); indexer.index_text(title); indexer.increase_termpos(); indexer.index_text(desc); doc.set_data(num.str() + "\n" + title + "\n" + desc); doc.add_value(0, xapian::sortable_serialise(i)); string idterm = "q" + num.str(); doc.add_boolean_term(idterm); db.add_document(doc); } db.commit(); } catch (const xapian::error &e) { cout << e.get_description() << endl; return 1; } return 0; } int search() { try { xapian::database db("./index.db"); xapian::enquire enquire(db); xapian::query queryleft(xapian::query::op_value_range, 0, xapian::sortable_serialise(1), xapian::sortable_serialise(2)); xapian::query queryright(xapian::query::op_value_range, 0, xapian::sortable_serialise(8), xapian::sortable_serialise(11)); xapian::query query(xapian::query::op_or, queryleft, queryright); cout << "parsed query is: " << query.get_description() << endl; enquire.set_query(query); xapian::mset matches = enquire.get_mset(0, 10); cout << matches.get_matches_estimated() << " results found.\n"; cout << "matches 1-" << matches.size() << ":\n" << endl; (xapian::msetiterator = matches.begin(); != matches.end(); ++i) { cout << i.get_rank() + 1 << ":" << i.get_percent() << "%" << endl; cout << i.get_document().get_data() << endl << endl; } } catch (const xapian::error &e) { cout << e.get_description() << endl; return 1; } return 0; } int main() { index(); search(); }
following output:
parsed query is: xapian::query((value_range 0 � � or value_range 0 � ��)) 6 results found. matches 1-6: 1:100% 1 title 1 example data number of 1 2:100% 2 title 2 example data number of 2 3:100% 8 title 8 example data number of 8 4:100% 9 title 9 example data number of 9 5:100% 10 title 10 example data number of 10 6:100% 11 title 11 example data number of 11
Comments
Post a Comment