examples/quest.cc

Go to the documentation of this file.
00001 /* quest.cc - Command line search tool using Xapian::QueryParser.
00002  *
00003  * Copyright (C) 2004,2005,2006,2007,2008 Olly Betts
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00018  * USA
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 // Stopwords:
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 ':': // missing parameter
00112                 case '?': // unknown option
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 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.