2 * @brief Simple example program demonstrating query expansion.
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
30 #include <cstdlib> // For exit().
36 main(int argc
, char **argv
)
38 // We require at least two command line arguments.
42 if (strcmp(argv
[1], "--version") == 0) {
43 cout
<< "simpleexpand" << endl
;
46 if (strcmp(argv
[1], "--help") == 0) {
50 cout
<< "Usage: " << argv
[0] << " PATH_TO_DATABASE QUERY [-- [DOCID...]]" << endl
;
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
63 string
query_string(argv
[2]);
65 while (*argv
&& strcmp(*argv
, "--") != 0) {
67 query_string
+= *argv
++;
70 // Create an RSet with the listed docids in.
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
);
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).
103 Xapian::MSetIterator i
= matches
.begin();
104 while (c
-- && i
!= matches
.end()) {
105 rset
.add_document(*i
);
110 // Generate an ESet containing terms that the user might want to add to
112 Xapian::ESet eset
= enquire
.get_eset(10, rset
);
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
;