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

Revision 8842, 2.5 kB (checked in by olly, 19 months ago)

examples/simplesearch.cc: Report "Matches 1-<N>:".

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/** @file simplesearch.cc
2 * @brief Simple command-line search utility.
3 *
4 * See "quest" for a more sophisticated example.
5 */
6/* Copyright (C) 2007 Olly Betts
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 */
22
23#include <xapian.h>
24
25#include <iostream>
26#include <string>
27
28#include <stdlib.h> // For exit().
29
30using namespace std;
31
32int
33main(int argc, char **argv)
34try {
35    // We require at least two command line arguments.
36    if (argc < 3) {
37        cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY" << endl;
38        exit(1);
39    }
40
41    // Open the database for searching.
42    Xapian::Database db(argv[1]);
43
44    // Start an enquire session.
45    Xapian::Enquire enquire(db);
46
47    // Combine the rest of the command line arguments with spaces between
48    // them, so that simple queries don't have to be quoted at the shell
49    // level.
50    string query_string(argv[2]);
51    argv += 3;
52    while (*argv) {
53        query_string += ' ';
54        query_string += *argv++;
55    }
56
57    // Parse the query string to produce a Xapian::Query object.
58    Xapian::QueryParser qp;
59    Xapian::Stem stemmer("english");
60    qp.set_stemmer(stemmer);
61    qp.set_database(db);
62    qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME);
63    Xapian::Query query = qp.parse_query(query_string);
64    cout << "Parsed query is: " << query.get_description() << endl;
65
66    // Find the top 10 results for the query.
67    enquire.set_query(query);
68    Xapian::MSet matches = enquire.get_mset(0, 10);
69
70    // Display the results.
71    cout << matches.get_matches_estimated() << " results found.\n";
72    cout << "Matches 1-" << matches.size() << ":\n" << endl;
73
74    for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
75        cout << i.get_rank() + 1 << ": " << i.get_percent() << "% docid=" << *i
76             << " [" << i.get_document().get_data() << "]\n\n";
77    }
78} catch (const Xapian::Error &e) {
79    cout << e.get_description() << endl;
80    exit(1);
81}
Note: See TracBrowser for help on using the browser.