00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <xapian.h>
00022
00023 #include <iostream>
00024 #include <string>
00025
00026 #include <cstdlib>
00027 #include <cstring>
00028
00029 using namespace std;
00030
00031 int
00032 main(int argc, char **argv)
00033 try {
00034
00035 if (argc < 3) {
00036 int rc = 1;
00037 if (argv[1]) {
00038 if (strcmp(argv[1], "--version") == 0) {
00039 cout << "simpleexpand" << endl;
00040 exit(0);
00041 }
00042 if (strcmp(argv[1], "--help") == 0) {
00043 rc = 0;
00044 }
00045 }
00046 cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY [-- [DOCID...]]" << endl;
00047 exit(rc);
00048 }
00049
00050
00051 Xapian::Database db(argv[1]);
00052
00053
00054 Xapian::Enquire enquire(db);
00055
00056
00057
00058
00059 string query_string(argv[2]);
00060 argv += 3;
00061 while (*argv && strcmp(*argv, "--") != 0) {
00062 query_string += ' ';
00063 query_string += *argv++;
00064 }
00065
00066
00067 Xapian::RSet rset;
00068 if (*argv) {
00069 while (*++argv) {
00070 rset.add_document(atoi(*argv));
00071 }
00072 }
00073
00074
00075 Xapian::QueryParser qp;
00076 Xapian::Stem stemmer("english");
00077 qp.set_stemmer(stemmer);
00078 qp.set_database(db);
00079 qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
00080 Xapian::Query query = qp.parse_query(query_string);
00081 cout << "Parsed query is: " << query.get_description() << endl;
00082
00083
00084 enquire.set_query(query);
00085 Xapian::MSet matches = enquire.get_mset(0, 10, &rset);
00086
00087
00088 cout << matches.get_matches_estimated() << " results found:" << endl;
00089
00090 for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
00091 cout << i.get_rank() + 1 << ": " << i.get_percent() << "% docid=" << *i
00092 << " [" << i.get_document().get_data() << "]\n\n";
00093 }
00094
00095
00096
00097 if (rset.empty()) {
00098 int c = 5;
00099 Xapian::MSetIterator i = matches.begin();
00100 while (c-- && i != matches.end()) {
00101 rset.add_document(*i);
00102 ++i;
00103 }
00104 }
00105
00106
00107
00108 Xapian::ESet eset = enquire.get_eset(10, rset);
00109
00110
00111 Xapian::ESetIterator t;
00112 for (t = eset.begin(); t != eset.end(); ++t) {
00113 cout << *t << ": weight = " << t.get_weight() << endl;
00114 }
00115 } catch (const Xapian::Error &e) {
00116 cout << e.get_description() << endl;
00117 exit(1);
00118 }