scriptindex: Finish parsing index script after error
[xapian.git] / xapian-core / examples / simpleexpand.cc
blob92ab7b4a35e603c3a5b666435a5ef277e7ea74df
1 /** @file
2 * @brief Simple example program demonstrating query expansion.
3 */
4 /* Copyright (C) 2007,2010,2015 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <xapian.h>
27 #include <iostream>
28 #include <string>
30 #include <cstdlib> // For exit().
31 #include <cstring>
33 using namespace std;
35 int
36 main(int argc, char **argv)
37 try {
38 // We require at least two command line arguments.
39 if (argc < 3) {
40 int rc = 1;
41 if (argv[1]) {
42 if (strcmp(argv[1], "--version") == 0) {
43 cout << "simpleexpand" << endl;
44 exit(0);
46 if (strcmp(argv[1], "--help") == 0) {
47 rc = 0;
50 cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY [-- [DOCID...]]" << endl;
51 exit(rc);
54 // Open the database for searching.
55 Xapian::Database db(argv[1]);
57 // Start an enquire session.
58 Xapian::Enquire enquire(db);
60 // Combine command line arguments up to "--" with spaces between
61 // them, so that simple queries don't have to be quoted at the shell
62 // level.
63 string query_string(argv[2]);
64 argv += 3;
65 while (*argv && strcmp(*argv, "--") != 0) {
66 query_string += ' ';
67 query_string += *argv++;
70 // Create an RSet with the listed docids in.
71 Xapian::RSet rset;
72 if (*argv) {
73 while (*++argv) {
74 rset.add_document(atoi(*argv));
78 // Parse the query string to produce a Xapian::Query object.
79 Xapian::QueryParser qp;
80 Xapian::Stem stemmer("english");
81 qp.set_stemmer(stemmer);
82 qp.set_database(db);
83 qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
84 Xapian::Query query = qp.parse_query(query_string);
85 cout << "Parsed query is: " << query.get_description() << endl;
87 // Find the top 10 results for the query.
88 enquire.set_query(query);
89 Xapian::MSet matches = enquire.get_mset(0, 10, &rset);
91 // Display the results.
92 cout << matches.get_matches_estimated() << " results found:" << endl;
94 for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
95 cout << i.get_rank() + 1 << ": " << i.get_weight() << " docid=" << *i
96 << " [" << i.get_document().get_data() << "]\n\n";
99 // If no relevant docids were given, invent an RSet containing the top 5
100 // matches (or all the matches if there are less than 5).
101 if (rset.empty()) {
102 int c = 5;
103 Xapian::MSetIterator i = matches.begin();
104 while (c-- && i != matches.end()) {
105 rset.add_document(*i);
106 ++i;
110 // Generate an ESet containing terms that the user might want to add to
111 // the query.
112 Xapian::ESet eset = enquire.get_eset(10, rset);
114 // List the terms.
115 Xapian::ESetIterator t;
116 for (t = eset.begin(); t != eset.end(); ++t) {
117 cout << *t << ": weight = " << t.get_weight() << endl;
119 } catch (const Xapian::Error &e) {
120 cout << e.get_description() << endl;
121 exit(1);