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,2008,2009 Olly Betts
00007  * Copyright 2006,2007,2008,2009 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/types.h>
00033 #include <xapian/termiterator.h>
00034 #include <xapian/visibility.h>
00035 
00036 // FIXME: sort this out so we avoid exposing Xapian::Query::Internal
00037 // - we need to at present so that the Xapian::Query's template ctors
00038 // compile.
00039 class LocalSubMatch;
00040 class MultiMatch;
00041 class QueryOptimiser;
00042 struct SortPosName;
00043 
00044 namespace Xapian {
00045 
00046 class PostingSource;
00047 class SerialisationContext;
00048 
00053 class XAPIAN_VISIBILITY_DEFAULT Query {
00054     public:
00056         class Internal;
00058         Xapian::Internal::RefCntPtr<Internal> internal;
00059 
00061         typedef enum {
00063             OP_AND,
00064 
00066             OP_OR,
00067 
00069             OP_AND_NOT,
00070 
00072             OP_XOR,
00073 
00075             OP_AND_MAYBE,
00076 
00078             OP_FILTER,
00079 
00088             OP_NEAR,
00089 
00098             OP_PHRASE,
00099 
00101             OP_VALUE_RANGE,
00102 
00111             OP_SCALE_WEIGHT,
00112 
00116             OP_ELITE_SET,
00117 
00119             OP_VALUE_GE,
00120 
00122             OP_VALUE_LE
00123         } op;
00124 
00126         Query(const Query & copyme);
00127 
00129         Query & operator=(const Query & copyme);
00130 
00139         Query();
00140 
00142         ~Query();
00143 
00145         Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00146               Xapian::termpos pos_ = 0);
00147 
00149         Query(Query::op op_, const Query & left, const Query & right);
00150 
00152         Query(Query::op op_,
00153               const std::string & left, const std::string & right);
00154 
00170         template <class Iterator>
00171         Query(Query::op op_, Iterator qbegin, Iterator qend,
00172               Xapian::termcount parameter = 0);
00173 
00177         Query(Query::op op_, Xapian::Query q, double parameter);
00178 
00192         Query(Query::op op_, Xapian::valueno valno,
00193               const std::string &begin, const std::string &end);
00194 
00206         Query(Query::op op_, Xapian::valueno valno, const std::string &value);
00207 
00218         explicit Query(Xapian::PostingSource * external_source);
00219 
00221         static Xapian::Query MatchAll;
00222 
00224         static Xapian::Query MatchNothing;
00225 
00230         Xapian::termcount get_length() const;
00231 
00237         TermIterator get_terms_begin() const;
00238 
00242         TermIterator get_terms_end() const {
00243             return TermIterator(NULL);
00244         }
00245 
00249         bool empty() const;
00250 
00258         std::string serialise() const;
00259 
00267         static Query unserialise(const std::string &s);
00268 
00279         static Query unserialise(const std::string & s,
00280                                  const SerialisationContext & context);
00281 
00283         std::string get_description() const;
00284 
00285     private:
00286         void add_subquery(const Query & subq);
00287         void add_subquery(const Query * subq);
00288         void add_subquery(const std::string & tname);
00289         void start_construction(Query::op op_, Xapian::termcount parameter);
00290         void end_construction();
00291         void abort_construction();
00292 };
00293 
00294 template <class Iterator>
00295 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00296     : internal(0)
00297 {
00298     try {
00299         start_construction(op_, parameter);
00300 
00301         /* Add all the elements */
00302         while (qbegin != qend) {
00303             add_subquery(*qbegin);
00304             ++qbegin;
00305         }
00306 
00307         end_construction();
00308     } catch (...) {
00309         abort_construction();
00310         throw;
00311     }
00312 }
00313 
00314 #ifndef SWIG // SWIG has no interest in the internal class, so hide it completely.
00315 
00317 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00318     friend class ::LocalSubMatch;
00319     friend class ::MultiMatch;
00320     friend class ::QueryOptimiser;
00321     friend struct ::SortPosName;
00322     friend class Query;
00323     public:
00324         static const int OP_LEAF = -1;
00325         static const int OP_EXTERNAL_SOURCE = -2;
00326 
00328         typedef std::vector<Internal *> subquery_list;
00329 
00331         typedef int op_t;
00332 
00333     private:
00335         Xapian::Query::Internal::op_t op;
00336 
00338         subquery_list subqs;
00339 
00347         Xapian::termcount parameter;
00348 
00355         std::string tname;
00356 
00358         std::string str_parameter;
00359 
00361         Xapian::termpos term_pos;
00362 
00364         Xapian::termcount wqf;
00365 
00367         Xapian::PostingSource * external_source;
00368 
00370         bool external_source_owned;
00371 
00379         void swap(Query::Internal &other);
00380 
00382         void initialise_from_copy(const Query::Internal & copyme);
00383 
00384         void accumulate_terms(
00385             std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00386 
00391         Internal * simplify_query();
00392 
00398         void validate_query() const;
00399 
00405         bool simplify_matchnothing();
00406 
00409         static std::string get_op_name(Xapian::Query::Internal::op_t op);
00410 
00413         void collapse_subqs();
00414 
00418         Xapian::Query::Internal * flatten_subqs();
00419 
00422         std::string serialise(Xapian::termpos & curpos) const;
00423 
00424     public:
00426         Internal(const Query::Internal & copyme);
00427 
00429         void operator=(const Query::Internal & copyme);
00430 
00432         explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00433                           Xapian::termpos term_pos_ = 0);
00434 
00436         Internal(op_t op_, Xapian::termcount parameter);
00437 
00439         Internal(op_t op_, Xapian::valueno valno,
00440                  const std::string &begin, const std::string &end);
00441 
00444         Internal(op_t op_, Xapian::valueno valno, const std::string &value);
00445 
00447         explicit Internal(Xapian::PostingSource * external_source_, bool owned);
00448 
00450         ~Internal();
00451 
00452         static Xapian::Query::Internal * unserialise(const std::string &s,
00453                                                      const SerialisationContext & context);
00454 
00456         void add_subquery(const Query::Internal * subq);
00457 
00462         void add_subquery_nocopy(Query::Internal * subq);
00463 
00464         void set_dbl_parameter(double dbl_parameter_);
00465 
00466         double get_dbl_parameter() const;
00467 
00470         Query::Internal * end_construction();
00471 
00475         std::string serialise() const {
00476             Xapian::termpos curpos = 1;
00477             return serialise(curpos);
00478         }
00479 
00481         std::string get_description() const;
00482 
00490         Xapian::termcount get_parameter() const { return parameter; }
00491 
00496         Xapian::termcount get_length() const;
00497 
00503         TermIterator get_terms() const;
00504 };
00505 
00506 #endif // SWIG
00507 
00508 }
00509 
00510 #endif /* XAPIAN_INCLUDED_QUERY_H */

Documentation for Xapian (version 1.1.0).
Generated on 22 Apr 2009 by Doxygen 1.5.2.