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/deprecated.h>
00033 #include <xapian/types.h>
00034 #include <xapian/termiterator.h>
00035 #include <xapian/visibility.h>
00036
00037
00038
00039
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 } op;
00116
00118 Query(const Query & copyme);
00119
00121 Query & operator=(const Query & copyme);
00122
00131 Query();
00132
00134 ~Query();
00135
00137 Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00138 Xapian::termpos pos_ = 0);
00139
00141 Query(Query::op op_, const Query & left, const Query & right);
00142
00144 Query(Query::op op_,
00145 const std::string & left, const std::string & right);
00146
00162 template <class Iterator>
00163 Query(Query::op op_, Iterator qbegin, Iterator qend,
00164 Xapian::termcount parameter = 0);
00165
00172 XAPIAN_DEPRECATED(Query(Query::op op_, Xapian::Query q));
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
00196 static Xapian::Query MatchAll;
00197
00199 static Xapian::Query MatchNothing;
00200
00205 Xapian::termcount get_length() const;
00206
00212 TermIterator get_terms_begin() const;
00213
00217 TermIterator get_terms_end() const {
00218 return TermIterator(NULL);
00219 }
00220
00224 bool empty() const;
00225
00229 std::string get_description() const;
00230
00231 private:
00232 void add_subquery(const Query & subq);
00233 void add_subquery(const Query * subq);
00234 void add_subquery(const std::string & tname);
00235 void start_construction(Query::op op_, Xapian::termcount parameter);
00236 void end_construction();
00237 void abort_construction();
00238 };
00239
00240 template <class Iterator>
00241 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00242 : internal(0)
00243 {
00244 try {
00245 start_construction(op_, parameter);
00246
00247
00248 while (qbegin != qend) {
00249 add_subquery(*qbegin);
00250 ++qbegin;
00251 }
00252
00253 end_construction();
00254 } catch (...) {
00255 abort_construction();
00256 throw;
00257 }
00258 }
00259
00261 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00262 friend class ::LocalSubMatch;
00263 friend class ::MultiMatch;
00264 friend class ::QueryOptimiser;
00265 friend struct ::SortPosName;
00266 friend class Query;
00267 public:
00268 static const int OP_LEAF = -1;
00269
00271 typedef std::vector<Internal *> subquery_list;
00272
00274 typedef int op_t;
00275
00276 private:
00278 Xapian::Query::Internal::op_t op;
00279
00281 subquery_list subqs;
00282
00290 Xapian::termcount parameter;
00291
00297 std::string tname;
00298
00300 std::string str_parameter;
00301
00303 Xapian::termpos term_pos;
00304
00306 Xapian::termcount wqf;
00307
00315 void swap(Query::Internal &other);
00316
00318 void initialise_from_copy(const Query::Internal & copyme);
00319
00320 void accumulate_terms(
00321 std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00322
00327 Internal * simplify_query();
00328
00334 void validate_query() const;
00335
00341 bool simplify_matchnothing();
00342
00345 static std::string get_op_name(Xapian::Query::Internal::op_t op);
00346
00349 void collapse_subqs();
00350
00354 void flatten_subqs();
00355
00358 std::string serialise(Xapian::termpos & curpos) const;
00359
00360 public:
00362 Internal(const Query::Internal & copyme);
00363
00365 void operator=(const Query::Internal & copyme);
00366
00368 explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00369 Xapian::termpos term_pos_ = 0);
00370
00372 Internal(op_t op_, Xapian::termcount parameter);
00373
00375 Internal(op_t op_, Xapian::valueno valno,
00376 const std::string &begin, const std::string &end);
00377
00379 ~Internal();
00380
00381 static Xapian::Query::Internal * unserialise(const std::string &s);
00382
00385 void add_subquery(const Query::Internal * subq);
00386
00387 void set_dbl_parameter(double dbl_parameter_);
00388
00389 double get_dbl_parameter() const;
00390
00393 Query::Internal * end_construction();
00394
00398 std::string serialise() const {
00399 Xapian::termpos curpos = 1;
00400 return serialise(curpos);
00401 }
00402
00406 std::string get_description() const;
00407
00415 Xapian::termcount get_parameter() const { return parameter; }
00416
00421 Xapian::termcount get_length() const;
00422
00428 TermIterator get_terms() const;
00429 };
00430
00431 }
00432
00433 #endif