examples/simpleindex.cc

Go to the documentation of this file.
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 
00028 using namespace std;
00029 
00030 int
00031 main(int argc, char **argv)
00032 try {
00033     if (argc != 2) {
00034         cout << "Usage: " << argv[0] << " PATH_TO_DATABASE" << endl;
00035         exit(1);
00036     }
00037 
00038     // Open the database for update, creating a new database if necessary.
00039     Xapian::WritableDatabase db(argv[1], Xapian::DB_CREATE_OR_OPEN);
00040 
00041     Xapian::TermGenerator indexer;
00042     Xapian::Stem stemmer("english");
00043     indexer.set_stemmer(stemmer);
00044 
00045     string para;
00046     while (true) {
00047         string line;
00048         if (cin.eof()) {
00049             if (para.empty()) break;
00050         } else {
00051             getline(cin, line);
00052         }
00053 
00054         if (line.empty()) {
00055             if (!para.empty()) {
00056                 // We've reached the end of a paragraph, so index it.
00057                 Xapian::Document doc;
00058                 doc.set_data(para);
00059 
00060                 indexer.set_document(doc);
00061                 indexer.index_text(para);
00062 
00063                 // Add the document to the database.
00064                 db.add_document(doc);
00065 
00066                 para = "";
00067             }
00068         } else {
00069             if (!para.empty()) para += ' ';
00070             para += line;
00071         }
00072     }
00073 } catch (const Xapian::Error &e) {
00074     cout << e.get_description() << endl;
00075     exit(1);
00076 }

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.