00001 00006 /* Copyright (C) 2007 Olly Betts 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include <xapian.h> 00024 00025 #include <iostream> 00026 #include <string> 00027 00028 #include <stdlib.h> // For exit(). 00029 00030 using namespace std; 00031 00032 int 00033 main(int argc, char **argv) 00034 try { 00035 // We require at least two command line arguments. 00036 if (argc < 3) { 00037 cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY" << endl; 00038 exit(1); 00039 } 00040 00041 // Open the database for searching. 00042 Xapian::Database db(argv[1]); 00043 00044 // Start an enquire session. 00045 Xapian::Enquire enquire(db); 00046 00047 // Combine the rest of the command line arguments with spaces between 00048 // them, so that simple queries don't have to be quoted at the shell 00049 // level. 00050 string query_string(argv[2]); 00051 argv += 3; 00052 while (*argv) { 00053 query_string += ' '; 00054 query_string += *argv++; 00055 } 00056 00057 // Parse the query string to produce a Xapian::Query object. 00058 Xapian::QueryParser qp; 00059 Xapian::Stem stemmer("english"); 00060 qp.set_stemmer(stemmer); 00061 qp.set_database(db); 00062 qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME); 00063 Xapian::Query query = qp.parse_query(query_string); 00064 cout << "Parsed query is: " << query.get_description() << endl; 00065 00066 // Find the top 10 results for the query. 00067 enquire.set_query(query); 00068 Xapian::MSet matches = enquire.get_mset(0, 10); 00069 00070 // Display the results. 00071 cout << matches.get_matches_estimated() << " results found.\n"; 00072 cout << "Matches 1-" << matches.size() << ":\n" << endl; 00073 00074 for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) { 00075 cout << i.get_rank() + 1 << ": " << i.get_percent() << "% docid=" << *i 00076 << " [" << i.get_document().get_data() << "]\n\n"; 00077 } 00078 } catch (const Xapian::Error &e) { 00079 cout << e.get_description() << endl; 00080 exit(1); 00081 }