00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00037
00038
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
00139 OP_SYNONYM
00140 } op;
00141
00143 Query(const Query & copyme);
00144
00146 Query & operator=(const Query & copyme);
00147
00156 Query();
00157
00159 ~Query();
00160
00162 Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00163 Xapian::termpos pos_ = 0);
00164
00166 Query(Query::op op_, const Query & left, const Query & right);
00167
00169 Query(Query::op op_,
00170 const std::string & left, const std::string & right);
00171
00187 template <class Iterator>
00188 Query(Query::op op_, Iterator qbegin, Iterator qend,
00189 Xapian::termcount parameter = 0);
00190
00194 Query(Query::op op_, Xapian::Query q, double parameter);
00195
00209 Query(Query::op op_, Xapian::valueno valno,
00210 const std::string &begin, const std::string &end);
00211
00223 Query(Query::op op_, Xapian::valueno valno, const std::string &value);
00224
00235 explicit Query(Xapian::PostingSource * external_source);
00236
00238 static const Xapian::Query MatchAll;
00239
00241 static const Xapian::Query MatchNothing;
00242
00247 Xapian::termcount get_length() const;
00248
00254 TermIterator get_terms_begin() const;
00255
00259 TermIterator get_terms_end() const {
00260 return TermIterator(NULL);
00261 }
00262
00266 bool empty() const;
00267
00275 std::string serialise() const;
00276
00284 static Query unserialise(const std::string &s);
00285
00296 static Query unserialise(const std::string & s,
00297 const SerialisationContext & context);
00298
00300 std::string get_description() const;
00301
00302 private:
00303 void add_subquery(const Query & subq);
00304 void add_subquery(const Query * subq);
00305 void add_subquery(const std::string & tname);
00306 void start_construction(Query::op op_, Xapian::termcount parameter);
00307 void end_construction();
00308 void abort_construction();
00309 };
00310
00311 template <class Iterator>
00312 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00313 : internal(0)
00314 {
00315 try {
00316 start_construction(op_, parameter);
00317
00318
00319 while (qbegin != qend) {
00320 add_subquery(*qbegin);
00321 ++qbegin;
00322 }
00323
00324 end_construction();
00325 } catch (...) {
00326 abort_construction();
00327 throw;
00328 }
00329 }
00330
00331 #ifndef SWIG // SWIG has no interest in the internal class, so hide it completely.
00332
00334 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00335 friend class ::LocalSubMatch;
00336 friend class ::MultiMatch;
00337 friend class ::QueryOptimiser;
00338 friend struct ::SortPosName;
00339 friend class Query;
00340 public:
00341 static const int OP_LEAF = -1;
00342 static const int OP_EXTERNAL_SOURCE = -2;
00343
00345 typedef std::vector<Internal *> subquery_list;
00346
00348 typedef int op_t;
00349
00350 private:
00352 Xapian::Query::Internal::op_t op;
00353
00355 subquery_list subqs;
00356
00364 Xapian::termcount parameter;
00365
00372 std::string tname;
00373
00375 std::string str_parameter;
00376
00378 Xapian::termpos term_pos;
00379
00381 Xapian::termcount wqf;
00382
00384 Xapian::PostingSource * external_source;
00385
00387 bool external_source_owned;
00388
00396 void swap(Query::Internal &other);
00397
00399 void initialise_from_copy(const Query::Internal & copyme);
00400
00401 void accumulate_terms(
00402 std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00403
00408 Internal * simplify_query();
00409
00415 void validate_query() const;
00416
00422 bool simplify_matchnothing();
00423
00426 static std::string get_op_name(Xapian::Query::Internal::op_t op);
00427
00430 void collapse_subqs();
00431
00435 Xapian::Query::Internal * flatten_subqs();
00436
00439 std::string serialise(Xapian::termpos & curpos) const;
00440
00441 public:
00443 Internal(const Query::Internal & copyme);
00444
00446 void operator=(const Query::Internal & copyme);
00447
00449 explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00450 Xapian::termpos term_pos_ = 0);
00451
00453 Internal(op_t op_, Xapian::termcount parameter);
00454
00456 Internal(op_t op_, Xapian::valueno valno,
00457 const std::string &begin, const std::string &end);
00458
00461 Internal(op_t op_, Xapian::valueno valno, const std::string &value);
00462
00464 explicit Internal(Xapian::PostingSource * external_source_, bool owned);
00465
00467 ~Internal();
00468
00469 static Xapian::Query::Internal * unserialise(const std::string &s,
00470 const SerialisationContext & context);
00471
00473 void add_subquery(const Query::Internal * subq);
00474
00479 void add_subquery_nocopy(Query::Internal * subq);
00480
00481 void set_dbl_parameter(double dbl_parameter_);
00482
00483 double get_dbl_parameter() const;
00484
00487 Query::Internal * end_construction();
00488
00492 std::string serialise() const {
00493 Xapian::termpos curpos = 1;
00494 return serialise(curpos);
00495 }
00496
00498 std::string get_description() const;
00499
00507 Xapian::termcount get_parameter() const { return parameter; }
00508
00513 Xapian::termcount get_length() const;
00514
00520 TermIterator get_terms() const;
00521 };
00522
00523 #endif // SWIG
00524
00525 }
00526
00527 #endif