- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# include/xapian/queryparser.h

Go to the documentation of this file.

    00001
    00004 /* Copyright (C) 2005,2006,2007 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/termiterator.h>
    00028 #include <xapian/visibility.h>
    00029
    00030 #include <set>
    00031 #include <string>
    00032
    00033 namespace Xapian {
    00034
    00035 class Stem;
    00036
    00038 class XAPIAN_VISIBILITY_DEFAULT Stopper {
    00039   public:
    00041     virtual bool operator()(const std::string & term) const = 0;
    00042
    00044     virtual ~Stopper() { }
    00045
    00047     virtual std::string get_description() const;
    00048 };
    00049
    00051 class XAPIAN_VISIBILITY_DEFAULT SimpleStopper : public Stopper {
    00052   private:
    00053     std::set<std::string> stop_words;
    00054
    00055   public:
    00057     SimpleStopper() { }
    00058
    00060 #ifndef __SUNPRO_CC
    00061     template <class Iterator>
    00062     SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
    00063 #else
    00064     // Sun's C++ doesn't cope with the Iterator pointing to const char *.
    00065     template <class Iterator>
    00066     SimpleStopper(Iterator begin, Iterator end) {
    00067         while (begin != end) stop_words.insert(*begin++);
    00068     }
    00069 #endif
    00070
    00072     void add(const std::string & word) { stop_words.insert(word); }
    00073
    00075     virtual bool operator()(const std::string & term) const {
    00076         return stop_words.find(term) != stop_words.end();
    00077     }
    00078
    00080     virtual ~SimpleStopper() { }
    00081
    00083     virtual std::string get_description() const;
    00084 };
    00085
    00087 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
    00089     virtual ~ValueRangeProcessor();
    00090
    00097     virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
    00098 };
    00099
    00104 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
    00105     Xapian::valueno valno;
    00106
    00107   public:
    00112     StringValueRangeProcessor(Xapian::valueno valno_)
    00113         : valno(valno_) { }
    00114
    00116     Xapian::valueno operator()(std::string &, std::string &) {
    00117         return valno;
    00118     }
    00119 };
    00120
    00125 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public ValueRangeProcessor {
    00126     Xapian::valueno valno;
    00127     bool prefer_mdy;
    00128     int epoch_year;
    00129
    00130   public:
    00141     DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
    00142                             int epoch_year_ = 1970)
    00143         : valno(valno_), prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
    00144
    00151     Xapian::valueno operator()(std::string &begin, std::string &end);
    00152 };
    00153
    00154 // Xapian 1.0.0 and 1.0.1 had a conceptually broken implementation of
    00155 // NumberValueRangeProcessor which we quickly told people to avoid using.  But to keep
    00156 // ABI compatibility, we should keep it around until the next incompatible ABI change
    00157 // (probably 1.1.0).  So we put the new NumberValueRangeProcessor in a subnamespace and
    00158 // then pull it into this one with "using v102::NumberValueRangeProcessor".  The old
    00159 // NumberValueRangeProcessor still exists with default visibility, but isn't
    00160 // declared in an external or internal header, so will only be used when dynamically
    00161 // linking with application code built against Xapian 1.0.0 or 1.0.1.
    00162 namespace v102 {
    00163
    00171 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
    00172     Xapian::valueno valno;
    00173     bool prefix;
    00174     std::string str;
    00175
    00176   public:
    00181     NumberValueRangeProcessor(Xapian::valueno valno_)
    00182         : valno(valno_), prefix(false) { }
    00183
    00216     NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
    00217                               bool prefix_ = true)
    00218         : valno(valno_), prefix(prefix_), str(str_) { }
    00219
    00229     Xapian::valueno operator()(std::string &begin, std::string &end);
    00230 };
    00231
    00232 }
    00233
    00234 #ifndef XAPIAN_NO_V102_NUMBER_VRP
    00235 using v102::NumberValueRangeProcessor;
    00236 #endif
    00237
    00239 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
    00240   public:
    00242     class Internal;
    00244     Xapian::Internal::RefCntPtr<Internal> internal;
    00245
    00247     typedef enum {
    00249         FLAG_BOOLEAN = 1,
    00251         FLAG_PHRASE = 2,
    00253         FLAG_LOVEHATE = 4,
    00255         FLAG_BOOLEAN_ANY_CASE = 8,
    00261         FLAG_WILDCARD = 16,
    00268         FLAG_PURE_NOT = 32,
    00280         FLAG_PARTIAL = 64,
    00281
    00295         FLAG_SPELLING_CORRECTION = 128,
    00296
    00301         FLAG_SYNONYM = 256,
    00302
    00307         FLAG_AUTO_SYNONYMS = 512,
    00308
    00314         FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS
    00315     } feature_flag;
    00316
    00317     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
    00318
    00320     QueryParser(const QueryParser & o);
    00321
    00323     QueryParser & operator=(const QueryParser & o);
    00324
    00326     QueryParser();
    00327
    00329     ~QueryParser();
    00330
    00339     void set_stemmer(const Xapian::Stem & stemmer);
    00340
    00355     void set_stemming_strategy(stem_strategy strategy);
    00356
    00358     void set_stopper(const Stopper *stop = NULL);
    00359
    00361     void set_default_op(Query::op default_op);
    00362
    00364     Query::op get_default_op() const;
    00365
    00367     void set_database(const Database &db);
    00368
    00378     Query parse_query(const std::string &query_string,
    00379                       unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
    00380                       const std::string &default_prefix = "");
    00381
    00394     void add_prefix(const std::string &field, const std::string &prefix);
    00395
    00422     void add_boolean_prefix(const std::string & field, const std::string &prefix);
    00423
    00425     TermIterator stoplist_begin() const;
    00426     TermIterator stoplist_end() const {
    00427         return TermIterator(NULL);
    00428     }
    00429
    00431     TermIterator unstem_begin(const std::string &term) const;
    00432     TermIterator unstem_end(const std::string &) const {
    00433         return TermIterator(NULL);
    00434     }
    00435
    00437     void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
    00438
    00446     std::string get_corrected_query_string() const;
    00447
    00449     std::string get_description() const;
    00450 };
    00451
    00476 XAPIAN_VISIBILITY_DEFAULT
    00477 std::string sortable_serialise(double value);
    00478
    00491 XAPIAN_VISIBILITY_DEFAULT
    00492 double sortable_unserialise(const std::string & value);
    00493
    00494 }
    00495
    00496 #endif // XAPIAN_INCLUDED_QUERYPARSER_H