# include/xapian/query.h

Go to the documentation of this file.

    00001
    00004 /* Copyright 1999,2000,2001 BrightStation PLC
    00005  * Copyright 2002 Ananova Ltd
    00006  * Copyright 2003,2004,2005,2006,2007 Olly Betts
    00007  * Copyright 2006,2007,2008 Lemur Consulting Ltd
    00008  *
    00009  * This program is free software; you can redistribute it and/or
    00010  * modify it under the terms of the GNU General Public License as
    00011  * published by the Free Software Foundation; either version 2 of the
    00012  * License, or (at your option) any later version.
    00013  *
    00014  * This program is distributed in the hope that it will be useful,
    00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00017  * GNU General Public License for more details.
    00018  *
    00019  * You should have received a copy of the GNU General Public License
    00020  * along with this program; if not, write to the Free Software
    00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
    00022  * USA
    00023  */
    00024
    00025 #ifndef XAPIAN_INCLUDED_QUERY_H
    00026 #define XAPIAN_INCLUDED_QUERY_H
    00027
    00028 #include <string>
    00029 #include <vector>
    00030
    00031 #include <xapian/base.h>
    00032 #include <xapian/deprecated.h>
    00033 #include <xapian/types.h>
    00034 #include <xapian/termiterator.h>
    00035 #include <xapian/visibility.h>
    00036
    00037 // FIXME: sort this out so we avoid exposing Xapian::Query::Internal
    00038 // - we need to at present so that the Xapian::Query's template ctors
    00039 // compile.
    00040 class LocalSubMatch;
    00041 class MultiMatch;
    00042 class QueryOptimiser;
    00043 struct SortPosName;
    00044
    00045 namespace Xapian {
    00046
    00051 class XAPIAN_VISIBILITY_DEFAULT Query {
    00052     public:
    00054         class Internal;
    00056         Xapian::Internal::RefCntPtr<Internal> internal;
    00057
    00059         typedef enum {
    00061             OP_AND,
    00062
    00064             OP_OR,
    00065
    00067             OP_AND_NOT,
    00068
    00070             OP_XOR,
    00071
    00073             OP_AND_MAYBE,
    00074
    00076             OP_FILTER,
    00077
    00086             OP_NEAR,
    00087
    00096             OP_PHRASE,
    00097
    00099             OP_VALUE_RANGE,
    00100
    00109             OP_SCALE_WEIGHT,
    00110
    00114             OP_ELITE_SET,
    00115
    00117             OP_VALUE_GE,
    00118
    00120             OP_VALUE_LE
    00121         } op;
    00122
    00124         Query(const Query & copyme);
    00125
    00127         Query & operator=(const Query & copyme);
    00128
    00137         Query();
    00138
    00140         ~Query();
    00141
    00143         Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00144               Xapian::termpos pos_ = 0);
    00145
    00147         Query(Query::op op_, const Query & left, const Query & right);
    00148
    00150         Query(Query::op op_,
    00151               const std::string & left, const std::string & right);
    00152
    00168         template <class Iterator>
    00169         Query(Query::op op_, Iterator qbegin, Iterator qend,
    00170               Xapian::termcount parameter = 0);
    00171
    00178         XAPIAN_DEPRECATED(Query(Query::op op_, Xapian::Query q));
    00179
    00183         Query(Query::op op_, Xapian::Query q, double parameter);
    00184
    00198         Query(Query::op op_, Xapian::valueno valno,
    00199               const std::string &begin, const std::string &end);
    00200
    00212         Query(Query::op op_, Xapian::valueno valno, const std::string &value);
    00213
    00215         static Xapian::Query MatchAll;
    00216
    00218         static Xapian::Query MatchNothing;
    00219
    00224         Xapian::termcount get_length() const;
    00225
    00231         TermIterator get_terms_begin() const;
    00232
    00236         TermIterator get_terms_end() const {
    00237             return TermIterator(NULL);
    00238         }
    00239
    00243         bool empty() const;
    00244
    00246         std::string get_description() const;
    00247
    00248     private:
    00249         void add_subquery(const Query & subq);
    00250         void add_subquery(const Query * subq);
    00251         void add_subquery(const std::string & tname);
    00252         void start_construction(Query::op op_, Xapian::termcount parameter);
    00253         void end_construction();
    00254         void abort_construction();
    00255 };
    00256
    00257 template <class Iterator>
    00258 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
    00259     : internal(0)
    00260 {
    00261     try {
    00262         start_construction(op_, parameter);
    00263
    00264         /* Add all the elements */
    00265         while (qbegin != qend) {
    00266             add_subquery(*qbegin);
    00267             ++qbegin;
    00268         }
    00269
    00270         end_construction();
    00271     } catch (...) {
    00272         abort_construction();
    00273         throw;
    00274     }
    00275 }
    00276
    00278 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
    00279     friend class ::LocalSubMatch;
    00280     friend class ::MultiMatch;
    00281     friend class ::QueryOptimiser;
    00282     friend struct ::SortPosName;
    00283     friend class Query;
    00284     public:
    00285         static const int OP_LEAF = -1;
    00286
    00288         typedef std::vector<Internal *> subquery_list;
    00289
    00291         typedef int op_t;
    00292
    00293     private:
    00295         Xapian::Query::Internal::op_t op;
    00296
    00298         subquery_list subqs;
    00299
    00307         Xapian::termcount parameter;
    00308
    00315         std::string tname;
    00316
    00318         std::string str_parameter;
    00319
    00321         Xapian::termpos term_pos;
    00322
    00324         Xapian::termcount wqf;
    00325
    00333         void swap(Query::Internal &other);
    00334
    00336         void initialise_from_copy(const Query::Internal & copyme);
    00337
    00338         void accumulate_terms(
    00339             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
    00340
    00345         Internal * simplify_query();
    00346
    00352         void validate_query() const;
    00353
    00359         bool simplify_matchnothing();
    00360
    00363         static std::string get_op_name(Xapian::Query::Internal::op_t op);
    00364
    00367         void collapse_subqs();
    00368
    00372         void flatten_subqs();
    00373
    00376         std::string serialise(Xapian::termpos & curpos) const;
    00377
    00378     public:
    00380         Internal(const Query::Internal & copyme);
    00381
    00383         void operator=(const Query::Internal & copyme);
    00384
    00386         explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
    00387                           Xapian::termpos term_pos_ = 0);
    00388
    00390         Internal(op_t op_, Xapian::termcount parameter);
    00391
    00393         Internal(op_t op_, Xapian::valueno valno,
    00394                  const std::string &begin, const std::string &end);
    00395
    00398         Internal(op_t op_, Xapian::valueno valno, const std::string &value);
    00399
    00401         ~Internal();
    00402
    00403         static Xapian::Query::Internal * unserialise(const std::string &s);
    00404
    00407         void add_subquery(const Query::Internal * subq);
    00408
    00409         void set_dbl_parameter(double dbl_parameter_);
    00410
    00411         double get_dbl_parameter() const;
    00412
    00415         Query::Internal * end_construction();
    00416
    00420         std::string serialise() const {
    00421             Xapian::termpos curpos = 1;
    00422             return serialise(curpos);
    00423         }
    00424
    00426         std::string get_description() const;
    00427
    00435         Xapian::termcount get_parameter() const { return parameter; }
    00436
    00441         Xapian::termcount get_length() const;
    00442
    00448         TermIterator get_terms() const;
    00449 };
    00450
    00451 }
    00452
    00453 #endif /* XAPIAN_INCLUDED_QUERY_H */