include/xapian/queryparser.h

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2005,2006,2007,2008,2009 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     std::set<std::string> stop_words;
00053 
00054   public:
00056     SimpleStopper() { }
00057 
00059 #ifndef __SUNPRO_CC
00060     template <class Iterator>
00061     SimpleStopper(Iterator begin, Iterator end) : stop_words(begin, end) { }
00062 #else
00063     // Sun's C++ doesn't cope with the Iterator pointing to const char *.
00064     template <class Iterator>
00065     SimpleStopper(Iterator begin, Iterator end) {
00066         while (begin != end) stop_words.insert(*begin++);
00067     }
00068 #endif
00069 
00071     void add(const std::string & word) { stop_words.insert(word); }
00072 
00074     virtual bool operator()(const std::string & term) const {
00075         return stop_words.find(term) != stop_words.end();
00076     }
00077 
00079     virtual std::string get_description() const;
00080 };
00081 
00083 struct XAPIAN_VISIBILITY_DEFAULT ValueRangeProcessor {
00085     virtual ~ValueRangeProcessor();
00086 
00093     virtual Xapian::valueno operator()(std::string &begin, std::string &end) = 0;
00094 };
00095 
00100 class XAPIAN_VISIBILITY_DEFAULT StringValueRangeProcessor : public ValueRangeProcessor {
00101     Xapian::valueno valno;
00102 
00103   public:
00108     StringValueRangeProcessor(Xapian::valueno valno_)
00109         : valno(valno_) { }
00110 
00112     Xapian::valueno operator()(std::string &, std::string &) {
00113         return valno;
00114     }
00115 };
00116 
00121 class XAPIAN_VISIBILITY_DEFAULT DateValueRangeProcessor : public ValueRangeProcessor {
00122     Xapian::valueno valno;
00123     bool prefer_mdy;
00124     int epoch_year;
00125 
00126   public:
00137     DateValueRangeProcessor(Xapian::valueno valno_, bool prefer_mdy_ = false,
00138                             int epoch_year_ = 1970)
00139         : valno(valno_), prefer_mdy(prefer_mdy_), epoch_year(epoch_year_) { }
00140 
00147     Xapian::valueno operator()(std::string &begin, std::string &end);
00148 };
00149 
00157 class XAPIAN_VISIBILITY_DEFAULT NumberValueRangeProcessor : public ValueRangeProcessor {
00158     Xapian::valueno valno;
00159     bool prefix;
00160     std::string str;
00161 
00162   public:
00167     NumberValueRangeProcessor(Xapian::valueno valno_)
00168         : valno(valno_), prefix(false) { }
00169 
00202     NumberValueRangeProcessor(Xapian::valueno valno_, const std::string &str_,
00203                               bool prefix_ = true)
00204         : valno(valno_), prefix(prefix_), str(str_) { }
00205 
00215     Xapian::valueno operator()(std::string &begin, std::string &end);
00216 };
00217 
00219 class XAPIAN_VISIBILITY_DEFAULT QueryParser {
00220   public:
00222     class Internal;
00224     Xapian::Internal::RefCntPtr<Internal> internal;
00225 
00227     typedef enum {
00229         FLAG_BOOLEAN = 1,
00231         FLAG_PHRASE = 2,
00233         FLAG_LOVEHATE = 4,
00235         FLAG_BOOLEAN_ANY_CASE = 8,
00241         FLAG_WILDCARD = 16,
00248         FLAG_PURE_NOT = 32,
00260         FLAG_PARTIAL = 64,
00261 
00275         FLAG_SPELLING_CORRECTION = 128,
00276 
00281         FLAG_SYNONYM = 256,
00282 
00287         FLAG_AUTO_SYNONYMS = 512,
00288 
00294         FLAG_AUTO_MULTIWORD_SYNONYMS = 1024 | FLAG_AUTO_SYNONYMS,
00295 
00302         FLAG_DEFAULT = FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE
00303     } feature_flag;
00304 
00305     typedef enum { STEM_NONE, STEM_SOME, STEM_ALL } stem_strategy;
00306 
00308     QueryParser(const QueryParser & o);
00309 
00311     QueryParser & operator=(const QueryParser & o);
00312 
00314     QueryParser();
00315 
00317     ~QueryParser();
00318 
00327     void set_stemmer(const Xapian::Stem & stemmer);
00328 
00346     void set_stemming_strategy(stem_strategy strategy);
00347 
00349     void set_stopper(const Stopper *stop = NULL);
00350 
00352     void set_default_op(Query::op default_op);
00353 
00355     Query::op get_default_op() const;
00356 
00358     void set_database(const Database &db);
00359 
00369     Query parse_query(const std::string &query_string,
00370                       unsigned flags = FLAG_DEFAULT,
00371                       const std::string &default_prefix = std::string());
00372 
00408     void add_prefix(const std::string &field, const std::string &prefix);
00409 
00455     void add_boolean_prefix(const std::string & field, const std::string &prefix);
00456 
00458     TermIterator stoplist_begin() const;
00459     TermIterator stoplist_end() const {
00460         return TermIterator(NULL);
00461     }
00462 
00464     TermIterator unstem_begin(const std::string &term) const;
00465     TermIterator unstem_end(const std::string &) const {
00466         return TermIterator(NULL);
00467     }
00468 
00470     void add_valuerangeprocessor(Xapian::ValueRangeProcessor * vrproc);
00471 
00479     std::string get_corrected_query_string() const;
00480 
00482     std::string get_description() const;
00483 };
00484 
00509 XAPIAN_VISIBILITY_DEFAULT
00510 std::string sortable_serialise(double value);
00511 
00524 XAPIAN_VISIBILITY_DEFAULT
00525 double sortable_unserialise(const std::string & value);
00526 
00527 }
00528 
00529 #endif // XAPIAN_INCLUDED_QUERYPARSER_H

Documentation for Xapian (version 1.1.1).
Generated on 10 Jun 2009 by Doxygen 1.5.2.