include/xapian/queryparser.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2005,2006,2007,2008 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
00156 // to keep ABI compatibility, we should keep it around until the next
00157 // incompatible ABI change (probably 1.1.0).  So we put the new
00158 // NumberValueRangeProcessor in a subnamespace and then pull it into this one
00159 // with "using v102::NumberValueRangeProcessor".  The old
00160 // NumberValueRangeProcessor still exists with default visibility, but isn't
00161 // declared in an external or internal header, so will only be used when
00162 // dynamically linking with application code built against Xapian 1.0.0 or
00163 // 1.0.1.
00164 #ifndef DOXYGEN
00165 // Hide the v102 namespace from Doxygen as it isn't user-visible.
00166 namespace v102 {
00167 #endif
00168 
00176 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00177     Xapian::valueno valno;
00178     bool prefix;
00179     std::string str;
00180 
00181   public:
00186     NumberValueRangeProcessor(Xapian::valueno valno_)
00187         : valno(valno_), prefix(false) { }
00188 
00221     NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00222                               bool prefix_ = true)
00223         : valno(valno_), prefix(prefix_), str(str_) { }
00224 
00234     Xapian::valueno operator()(std::string &begin, std::string &end);
00235 };
00236 
00237 #ifndef DOXYGEN
00238 }
00239 
00240 # ifndef XAPIAN_NO_V102_NUMBER_VRP
00241 using v102::NumberValueRangeProcessor;
00242 # endif
00243 #endif
00244 
00246 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00247   public:
00249     class Internal;
00251     Xapian::Internal::RefCntPtr<Internal> internal;
00252 
00254     typedef enum {
00256         FLAG_BOOLEAN = 1,
00258         FLAG_PHRASE = 2,
00260         FLAG_LOVEHATE = 4,
00262         FLAG_BOOLEAN_ANY_CASE = 8,
00268         FLAG_WILDCARD = 16,
00275         FLAG_PURE_NOT = 32,
00287         FLAG_PARTIAL = 64,
00288 
00302         FLAG_SPELLING_CORRECTION = 128,
00303 
00308         FLAG_SYNONYM = 256,
00309 
00314         FLAG_AUTO_SYNONYMS = 512,
00315 
00321         FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS
00322     } feature_flag;
00323 
00324     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00325 
00327     QueryParser(const QueryParser & o);
00328 
00330     QueryParser & operator=(const QueryParser & o);
00331 
00333     QueryParser();
00334 
00336     ~QueryParser();
00337 
00346     void set_stemmer(const Xapian::Stem & stemmer);
00347 
00365     void set_stemming_strategy(stem_strategy strategy);
00366 
00368     void set_stopper(const Stopper *stop = NULL);
00369 
00371     void set_default_op(Query::op default_op);
00372 
00374     Query::op get_default_op() const;
00375 
00377     void set_database(const Database &db);
00378 
00388     Query parse_query(const std::string &query_string,
00389                       unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
00390                       const std::string &default_prefix = "");
00391 
00427     void add_prefix(const std::string &field, const std::string &prefix);
00428 
00474     void add_boolean_prefix(const std::string & field, const std::string &prefix);
00475 
00477     TermIterator stoplist_begin() const;
00478     TermIterator stoplist_end() const {
00479         return TermIterator(NULL);
00480     }
00481 
00483     TermIterator unstem_begin(const std::string &term) const;
00484     TermIterator unstem_end(const std::string &) const {
00485         return TermIterator(NULL);
00486     }
00487 
00489     void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00490 
00498     std::string get_corrected_query_string() const;
00499 
00501     std::string get_description() const;
00502 };
00503 
00528 XAPIAN_VISIBILITY_DEFAULT
00529 std::string sortable_serialise(double value);
00530 
00543 XAPIAN_VISIBILITY_DEFAULT
00544 double sortable_unserialise(const std::string & value);
00545 
00546 }
00547 
00548 #endif // XAPIAN_INCLUDED_QUERYPARSER_H

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