# include/xapian/queryparser.h

Go to the documentation of this file.

    00001
    00004 /* Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    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 };
    00044
    00046 class SimpleStopper : public Stopper {
    00047   private:
    00048     std::set<std::string> stop_words;
    00049
    00050   public:
    00052     SimpleStopper() { }
    00053
    00055 #ifndef __SUNPRO_CC
    00056     template <class Iterator>
    00057     SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
    00058 #else
    00059     // Sun's C++ doesn't cope with the Iterator points to const char *.
    00060     template <class Iterator>
    00061     SimpleStopper(Iterator begin, Iterator end) {
    00062         while (begin != end) stop_words.insert(*begin++);
    00063     }
    00064 #endif
    00065
    00067     void add(const std::string word) { stop_words.insert(word); }
    00068
    00070     virtual bool operator()(const std::string & term) const {
    00071         return stop_words.find(term) != stop_words.end();
    00072     }
    00073
    00075     virtual ~SimpleStopper() { }
    00076 };
    00077
    00079 class QueryParser {
    00080   public:
    00082     class Internal;
    00084     Xapian::Internal::RefCntPtr<Internal> internal;
    00085
    00087     typedef enum {
    00089         FLAG_BOOLEAN = 1,
    00091         FLAG_PHRASE = 2,
    00092         // Support + and -.
    00093         FLAG_LOVEHATE = 4,
    00094         // Support AND, OR, etc even if they aren't in ALLCAPS.
    00095         FLAG_BOOLEAN_ANY_CASE = 8,
    00096         // Support right truncation (e.g. Xap*).
    00097         FLAG_WILDCARD = 16
    00098     } feature_flag;
    00099
    00100     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
    00101
    00103     QueryParser(const QueryParser & o);
    00104
    00106     QueryParser & operator=(const QueryParser & o);
    00107
    00109     QueryParser();
    00110
    00112     ~QueryParser();
    00113
    00115     void set_stemmer(const Xapian::Stem & stemmer);
    00116
    00118     void set_stemming_strategy(stem_strategy strategy);
    00119
    00121     void set_stopper(const Stopper *stop = NULL);
    00122
    00124     void set_stemming_options(const std::string &lang, bool stem_all = false,
    00125                               const Stopper *stop = NULL) {
    00126         set_stemmer(Xapian::Stem(lang));
    00127         if (lang.empty() || lang == "none") {
    00128             set_stemming_strategy(STEM_NONE);
    00129         } else {
    00130             set_stemming_strategy(stem_all ? STEM_ALL : STEM_SOME);
    00131         }
    00132         set_stopper(stop);
    00133     }
    00134
    00136     void set_default_op(Query::op default_op);
    00137
    00139     Query::op get_default_op() const;
    00140
    00142     void set_database(const Database &db);
    00143
    00145     Query parse_query(const std::string &query_string,
    00146                       unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE);
    00147
    00160     void add_prefix(const std::string &field, const std::string &prefix);
    00161
    00177     void add_boolean_prefix(const std::string & field, const std::string &prefix);
    00178
    00180     TermIterator stoplist_begin() const;
    00181     TermIterator stoplist_end() const {
    00182         return TermIterator(NULL);
    00183     }
    00184
    00186     TermIterator unstem_begin(const std::string &term) const;
    00187     TermIterator unstem_end(const std::string &) const {
    00188         return TermIterator(NULL);
    00189     }
    00190
    00192     std::string get_description() const;
    00193 };
    00194
    00195 }
    00196
    00197 #endif // XAPIAN_INCLUDED_QUERYPARSER_H