cos-search.cxx
changeset 0 56281d90869c
child 3 38269b62d856
equal deleted inserted replaced
-1:000000000000 0:56281d90869c
       
     1 /* cos-search.cxx:
       
     2  *   based on quickstartsearch.cc: Simplest possible searcher
       
     3  *
       
     4  * ----START-LICENCE----
       
     5  * Copyright 1999,2000,2001 BrightStation PLC
       
     6  * Copyright 2003,2004 Olly Betts
       
     7  * Copyright 2010 Stiletto <blasux@blasux.ru>
       
     8  *
       
     9  * This program is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU General Public License as
       
    11  * published by the Free Software Foundation; either version 2 of the
       
    12  * License, or (at your option) any later version.
       
    13  *
       
    14  * This program is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with this program; if not, write to the Free Software
       
    21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
       
    22  * USA
       
    23  */
       
    24 
       
    25 #include <xapian.h>
       
    26 #include <iostream>
       
    27 #include <cstdlib>
       
    28 #include <algorithm>
       
    29 #include <pcrecpp.h>
       
    30 
       
    31 using namespace std;
       
    32 
       
    33 int main(int argc, char **argv)
       
    34 {
       
    35     // Simplest possible options parsing: we just require two or more
       
    36     // parameters.
       
    37     if (argc < 3) {
       
    38         cout << "usage: " << argv[0] <<
       
    39                 " <path to database> <search query>" << endl;
       
    40         exit(1);
       
    41     }
       
    42 
       
    43     // Catch any Xapian::Error exceptions thrown
       
    44     try {
       
    45 	Xapian::Database db(argv[1]);
       
    46         Xapian::Enquire enquire(db);
       
    47         Xapian::QueryParser qp;
       
    48         string qstring(argv[2]);
       
    49 
       
    50         pcrecpp::RE prereg("([A-Za-z0-9_-]+):");
       
    51         pcrecpp::StringPiece input(qstring);  // Wrap in a StringPiece
       
    52 
       
    53         string var;
       
    54         while (prereg.Consume(&input, &var)) {
       
    55             string upvar = var+"=";
       
    56             std::transform(var.begin(), var.end(), var.begin(), ::tolower);
       
    57             std::transform(upvar.begin(), upvar.end(), upvar.begin(), ::toupper);
       
    58             cout << var << upvar << endl;
       
    59             qp.add_boolean_prefix(var,upvar);
       
    60         }
       
    61 
       
    62 
       
    63         // Build the query object
       
    64         Xapian::Query query = qp.parse_query(qstring); //Xapian::Query::OP_OR, argv + 2, argv + argc);
       
    65         
       
    66         cout << "Performing query `" << query.get_description() << "'" << endl;
       
    67 
       
    68         // Give the query object to the enquire session
       
    69         enquire.set_query(query);
       
    70 
       
    71         // Get the top 10 results of the query
       
    72         Xapian::MSet matches = enquire.get_mset(0, db.get_doccount());
       
    73 
       
    74         // Display the results
       
    75         cout << matches.size() << " results found" << endl;
       
    76 
       
    77         for (Xapian::MSetIterator i = matches.begin();
       
    78              i != matches.end();
       
    79              ++i) {
       
    80             Xapian::Document doc = i.get_document();
       
    81             cout << "Document ID " << *i << "\t" <<
       
    82                     i.get_percent() << "% [" <<
       
    83                     doc.get_data() << "]" << endl;
       
    84         }
       
    85     } catch(const Xapian::Error &error) {
       
    86         cout << "Exception: "  << error.get_msg() << endl;
       
    87     }
       
    88 }