00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <xapian.h>
00024
00025 #include <stdlib.h>
00026
00027 #include <iostream>
00028
00029 #include "gnu_getopt.h"
00030
00031 using namespace std;
00032
00033 #define PROG_NAME "quest"
00034 #define PROG_DESC "Xapian command line search tool"
00035
00036
00037 static const char * sw[] = {
00038 "a", "about", "an", "and", "are", "as", "at",
00039 "be", "by",
00040 "en",
00041 "for", "from",
00042 "how",
00043 "i", "in", "is", "it",
00044 "of", "on", "or",
00045 "that", "the", "this", "to",
00046 "was", "what", "when", "where", "which", "who", "why", "will", "with"
00047 };
00048
00049 static void show_usage() {
00050 cout << "Usage: "PROG_NAME" [OPTIONS] 'QUERY'\n"
00051 "NB: QUERY should be quoted to protect it from the shell.\n\n"
00052 "Options:\n"
00053 " -d, --db=DIRECTORY database to search (multiple databases may be specified)\n"
00054 " -m, --msize=MSIZE maximum number of matches to return\n"
00055 " -s, --stemmer=LANG set the stemming language, the default is 'english'\n"
00056 " (pass 'none' to disable stemming)\n"
00057 " -h, --help display this help and exit\n"
00058 " -v, --version output version information and exit\n";
00059 }
00060
00061 int
00062 main(int argc, char **argv)
00063 {
00064 const char * opts = "d:m:s:hv";
00065 static const struct option long_opts[] = {
00066 { "db", required_argument, 0, 'd' },
00067 { "msize", required_argument, 0, 'm' },
00068 { "stemmer", required_argument, 0, 's' },
00069 { "help", no_argument, 0, 'h' },
00070 { "version", no_argument, 0, 'v' },
00071 { NULL, 0, 0, 0}
00072 };
00073
00074 Xapian::SimpleStopper mystopper(sw, sw + sizeof(sw) / sizeof(sw[0]));
00075 Xapian::Stem stemmer("english");
00076 int msize = 10;
00077
00078 bool have_database = false;
00079
00080 try {
00081 Xapian::Database db;
00082
00083 int c;
00084 while ((c = gnu_getopt_long(argc, argv, opts, long_opts, 0)) != -1) {
00085 switch (c) {
00086 case 'm':
00087 msize = atoi(optarg);
00088 break;
00089 case 'd':
00090 db.add_database(Xapian::Database(optarg));
00091 have_database = true;
00092 break;
00093 case 's':
00094 try {
00095 stemmer = Xapian::Stem(optarg);
00096 } catch (const Xapian::Error &) {
00097 cerr << "Unknown stemming language '" << optarg
00098 << "'.\n"
00099 "Available language names are: "
00100 << Xapian::Stem::get_available_languages() << endl;
00101 exit(1);
00102 }
00103 break;
00104 case 'v':
00105 cout << PROG_NAME" - "PACKAGE_STRING << endl;
00106 exit(0);
00107 case 'h':
00108 cout << PROG_NAME" - "PROG_DESC"\n\n";
00109 show_usage();
00110 exit(0);
00111 case ':':
00112 case '?':
00113 show_usage();
00114 exit(1);
00115 }
00116 }
00117
00118 if (!have_database || argc - optind != 1) {
00119 show_usage();
00120 exit(1);
00121 }
00122
00123 Xapian::QueryParser parser;
00124 parser.set_database(db);
00125 parser.set_default_op(Xapian::Query::OP_OR);
00126 parser.set_stemmer(stemmer);
00127 parser.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
00128 parser.set_stopper(&mystopper);
00129
00130 Xapian::Query query = parser.parse_query(argv[optind]);
00131 cout << "Query: " << query.get_description() << endl;
00132
00133 Xapian::Enquire enquire(db);
00134 enquire.set_query(query);
00135
00136 Xapian::MSet mset = enquire.get_mset(0, msize);
00137
00138 cout << "MSet:" << endl;
00139 for (Xapian::MSetIterator i = mset.begin(); i != mset.end(); i++) {
00140 Xapian::Document doc = i.get_document();
00141 string data = doc.get_data();
00142 cout << *i << " [" << i.get_percent() << "%]\n" << data << "\n";
00143 }
00144 cout << flush;
00145 } catch (const Xapian::QueryParserError & e) {
00146 cout << "Couldn't parse query: " << e.get_msg() << endl;
00147 exit(1);
00148 } catch (const Xapian::Error & err) {
00149 cout << err.get_description() << endl;
00150 exit(1);
00151 }
00152 }