include/xapian/queryparser.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2005,2006 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (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
00019  * USA
00020  */
00021 
00022 #ifndef XAPIAN_INCLUDED_QUERYPARSER_H
00023 #define XAPIAN_INCLUDED_QUERYPARSER_H
00024 
00025 #include <xapian/base.h>
00026 #include <xapian/query.h>
00027 #include <xapian/stem.h>
00028 #include <xapian/termiterator.h>
00029 
00030 #include <set>
00031 #include <string>
00032 
00033 namespace Xapian {
00034 
00036 class Stopper {
00037   public:
00039     virtual bool operator()(const std::string & term) const = 0;
00040 
00042     virtual ~Stopper() { }
00043 
00045     virtual std::string get_description() const;
00046 };
00047 
00049 class SimpleStopper : public Stopper {
00050   private:
00051     std::set<std::string> stop_words;
00052 
00053   public:
00055     SimpleStopper() { }
00056 
00058 #ifndef __SUNPRO_CC
00059     template <class Iterator>
00060     SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
00061 #else
00062     // Sun's C++ doesn't cope with the Iterator points to const char *.
00063     template <class Iterator>
00064     SimpleStopper(Iterator begin, Iterator end) {
00065         while (begin != end) stop_words.insert(*begin++);
00066     }
00067 #endif
00068 
00070     void add(const std::string word) { stop_words.insert(word); }
00071 
00073     virtual bool operator()(const std::string & term) const {
00074         return stop_words.find(term) != stop_words.end();
00075     }
00076 
00078     virtual ~SimpleStopper() { }
00079 
00081     virtual std::string get_description() const;
00082 };
00083 
00085 class QueryParser {
00086   public:
00088     class Internal;
00090     Xapian::Internal::RefCntPtr<Internal> internal;
00091 
00093     typedef enum {
00095         FLAG_BOOLEAN = 1,
00097         FLAG_PHRASE = 2,
00099         FLAG_LOVEHATE = 4,
00101         FLAG_BOOLEAN_ANY_CASE = 8,
00103         FLAG_WILDCARD = 16
00104     } feature_flag;
00105 
00106     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00107 
00109     QueryParser(const QueryParser & o);
00110 
00112     QueryParser & operator=(const QueryParser & o);
00113 
00115     QueryParser();
00116 
00118     ~QueryParser();
00119 
00121     void set_stemmer(const Xapian::Stem & stemmer);
00122 
00124     void set_stemming_strategy(stem_strategy strategy);
00125 
00127     void set_stopper(const Stopper *stop = NULL);
00128 
00130     void set_stemming_options(const std::string &lang, bool stem_all = false,
00131                               const Stopper *stop = NULL) {
00132         set_stemmer(Xapian::Stem(lang));
00133         if (lang.empty() || lang == "none") {
00134             set_stemming_strategy(STEM_NONE);
00135         } else {
00136             set_stemming_strategy(stem_all ? STEM_ALL : STEM_SOME);
00137         }
00138         set_stopper(stop);
00139     }
00140 
00142     void set_default_op(Query::op default_op);
00143 
00145     Query::op get_default_op() const;
00146 
00148     void set_database(const Database &db);
00149 
00157     Query parse_query(const std::string &query_string,
00158                       unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE);
00159 
00172     void add_prefix(const std::string &field, const std::string &prefix);
00173 
00189     void add_boolean_prefix(const std::string & field, const std::string &prefix);
00190 
00192     TermIterator stoplist_begin() const;
00193     TermIterator stoplist_end() const {
00194         return TermIterator(NULL);
00195     }
00196 
00198     TermIterator unstem_begin(const std::string &term) const;
00199     TermIterator unstem_end(const std::string &) const {
00200         return TermIterator(NULL);
00201     }
00202 
00204     std::string get_description() const;
00205 };
00206 
00207 }
00208 
00209 #endif // XAPIAN_INCLUDED_QUERYPARSER_H

Documentation for Xapian (version 0.9.3).
Generated on 16 Feb 2006 by Doxygen 1.4.6.