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