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