root / tags / 1.0.8 / xapian-core / examples / quest.cc

Revision 11151, 4.2 kB (checked in by olly, 4 months ago)

Backport change from trunk:
examples/quest.cc: Fix to catch QueryParserError? instead of const
char * which Xapian < 1.0.0 threw instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* quest.cc - Command line search tool using Xapian::QueryParser.
2 *
3 * Copyright (C) 2004,2005,2006,2007,2008 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
18 * USA
19 */
20
21#include <config.h>
22
23#include <xapian.h>
24
25#include <stdlib.h>
26
27#include <iostream>
28
29#include "gnu_getopt.h"
30
31using namespace std;
32
33#define PROG_NAME "quest"
34#define PROG_DESC "Xapian command line search tool"
35
36// Stopwords:
37static const char * sw[] = {
38    "a", "about", "an", "and", "are", "as", "at",
39    "be", "by",
40    "en",
41    "for", "from",
42    "how",
43    "i", "in", "is", "it",
44    "of", "on", "or",
45    "that", "the", "this", "to",
46    "was", "what", "when", "where", "which", "who", "why", "will", "with"
47};
48
49static void show_usage() {
50    cout << "Usage: "PROG_NAME" [OPTIONS] 'QUERY'\n"
51"NB: QUERY should be quoted to protect it from the shell.\n\n"
52"Options:\n"
53"  -d, --db=DIRECTORY  database to search (multiple databases may be specified)\n"
54"  -m, --msize=MSIZE   maximum number of matches to return\n"
55"  -s, --stemmer=LANG  set the stemming language, the default is 'english'\n"
56"                      (pass 'none' to disable stemming)\n"
57"  -h, --help          display this help and exit\n"
58"  -v, --version       output version information and exit\n";
59}
60
61int
62main(int argc, char **argv)
63{
64    static const struct option long_opts[] = {
65        { "db",         required_argument, 0, 'd' },
66        { "msize",      required_argument, 0, 'm' },
67        { "stemmer",    required_argument, 0, 's' },
68        { "help",       no_argument, 0, 'h' },
69        { "version",    no_argument, 0, 'v' },
70        { NULL,         0, 0, 0}
71    };
72
73    Xapian::SimpleStopper mystopper(sw, sw + sizeof(sw) / sizeof(sw[0]));
74    Xapian::Stem stemmer("english");
75    int msize = 10;
76
77    bool have_database = false;
78
79    try {
80        Xapian::Database db;
81
82        int c;
83        while ((c = gnu_getopt_long(argc, argv, "hvm:d:s:", long_opts, 0)) != EOF)
84        {
85            switch (c) {
86                case 'm':
87                    msize = atoi(optarg);
88                    break;
89                case 'd':
90                    db.add_database(Xapian::Database(optarg));
91                    have_database = true;
92                    break;
93                case 's':
94                    try {
95                        stemmer = Xapian::Stem(optarg);
96                    } catch (const Xapian::Error &) {
97                        cerr << "Unknown stemming language '" << optarg << "'.\n";
98                        cerr << "Available language names are: "
99                            << Xapian::Stem::get_available_languages() << endl;
100                        exit(1);
101                    }
102                    break;
103                case 'v':
104                    cout << PROG_NAME" - "PACKAGE_STRING << endl;
105                    exit(0);
106                case 'h':
107                    cout << PROG_NAME" - "PROG_DESC"\n\n";
108                    show_usage();
109                    exit(0);
110                case ':': // missing parameter
111                case '?': // unknown option
112                    show_usage();
113                    exit(1);
114            }
115        }
116
117        if (!have_database || argc - optind != 1) {
118            show_usage();
119            exit(1);
120        }
121
122        Xapian::QueryParser parser;
123        parser.set_database(db);
124        parser.set_default_op(Xapian::Query::OP_OR);
125        parser.set_stemmer(stemmer);
126        parser.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
127        parser.set_stopper(&mystopper);
128
129        Xapian::Query query = parser.parse_query(argv[optind]);
130        cout << "Query: " << query.get_description() << endl;
131
132        Xapian::Enquire enquire(db);
133        enquire.set_query(query);
134
135        Xapian::MSet mset = enquire.get_mset(0, msize);
136
137        cout << "MSet:" << endl;
138        for (Xapian::MSetIterator i = mset.begin(); i != mset.end(); i++) {
139            Xapian::Document doc = i.get_document();
140            string data = doc.get_data();
141            cout << *i << " [" << i.get_percent() << "%]\n" << data << "\n";
142        }
143        cout << flush;
144    } catch (const Xapian::QueryParserError & e) {
145        cout << "Couldn't parse query: " << e.get_msg() << endl;
146        exit(1);
147    } catch (const Xapian::Error & err) {
148        cout << err.get_description() << endl;
149        exit(1);
150    }
151}
Note: See TracBrowser for help on using the browser.