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
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 namespace v102 {
00165 
00173 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00174     Xapian::valueno valno;
00175     bool prefix;
00176     std::string str;
00177 
00178   public:
00183     NumberValueRangeProcessor(Xapian::valueno valno_)
00184         : valno(valno_), prefix(false) { }
00185 
00218     NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00219                               bool prefix_ = true)
00220         : valno(valno_), prefix(prefix_), str(str_) { }
00221 
00231     Xapian::valueno operator()(std::string &begin, std::string &end);
00232 };
00233 
00234 }
00235 
00236 #ifndef XAPIAN_NO_V102_NUMBER_VRP
00237 using v102::NumberValueRangeProcessor;
00238 #endif
00239 
00241 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00242   public:
00244     class Internal;
00246     Xapian::Internal::RefCntPtr<Internal> internal;
00247 
00249     typedef enum {
00251         FLAG_BOOLEAN = 1,
00253         FLAG_PHRASE = 2,
00255         FLAG_LOVEHATE = 4,
00257         FLAG_BOOLEAN_ANY_CASE = 8,
00263         FLAG_WILDCARD = 16,
00270         FLAG_PURE_NOT = 32,
00282         FLAG_PARTIAL = 64,
00283 
00297         FLAG_SPELLING_CORRECTION = 128,
00298 
00303         FLAG_SYNONYM = 256,
00304 
00309         FLAG_AUTO_SYNONYMS = 512,
00310 
00316         FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS
00317     } feature_flag;
00318 
00319     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00320 
00322     QueryParser(const QueryParser & o);
00323 
00325     QueryParser & operator=(const QueryParser & o);
00326 
00328     QueryParser();
00329 
00331     ~QueryParser();
00332 
00341     void set_stemmer(const Xapian::Stem & stemmer);
00342 
00360     void set_stemming_strategy(stem_strategy strategy);
00361 
00363     void set_stopper(const Stopper *stop = NULL);
00364 
00366     void set_default_op(Query::op default_op);
00367 
00369     Query::op get_default_op() const;
00370 
00372     void set_database(const Database &db);
00373 
00383     Query parse_query(const std::string &query_string,
00384                       unsigned flags = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE,
00385                       const std::string &default_prefix = "");
00386 
00422     void add_prefix(const std::string &field, const std::string &prefix);
00423 
00469     void add_boolean_prefix(const std::string & field, const std::string &prefix);
00470 
00472     TermIterator stoplist_begin() const;
00473     TermIterator stoplist_end() const {
00474         return TermIterator(NULL);
00475     }
00476 
00478     TermIterator unstem_begin(const std::string &term) const;
00479     TermIterator unstem_end(const std::string &) const {
00480         return TermIterator(NULL);
00481     }
00482 
00484     void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00485 
00493     std::string get_corrected_query_string() const;
00494 
00496     std::string get_description() const;
00497 };
00498 
00523 XAPIAN_VISIBILITY_DEFAULT
00524 std::string sortable_serialise(double value);
00525 
00538 XAPIAN_VISIBILITY_DEFAULT
00539 double sortable_unserialise(const std::string & value);
00540 
00541 }
00542 
00543 #endif // XAPIAN_INCLUDED_QUERYPARSER_H

Documentation for Xapian (version 1.0.4).
Generated on 30 Oct 2007 by Doxygen 1.5.2.