00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef XAPIAN_INCLUDED_QUERY_H
00027 #define XAPIAN_INCLUDED_QUERY_H
00028
00029 #include <string>
00030 #include <vector>
00031
00032 #include <xapian/base.h>
00033 #include <xapian/types.h>
00034
00035
00036
00037
00038 class MultiMatch;
00039 class LocalSubMatch;
00040 class SortPosName;
00041
00042 namespace Xapian {
00043
00044 class TermIterator;
00045
00050 class Query {
00051 public:
00053 class Internal;
00055 Xapian::Internal::RefCntPtr<Internal> internal;
00056
00058 typedef enum {
00060 OP_AND,
00061
00063 OP_OR,
00064
00066 OP_AND_NOT,
00067
00069 OP_XOR,
00070
00072 OP_AND_MAYBE,
00073
00075 OP_FILTER,
00076
00085 OP_NEAR,
00086
00095 OP_PHRASE,
00096
00103 OP_WEIGHT_CUTOFF,
00104
00108 OP_ELITE_SET
00109 } op;
00110
00112 Query(const Query & copyme);
00113
00115 Query & operator=(const Query & copyme);
00116
00125 Query();
00126
00128 ~Query();
00129
00131 Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00132 Xapian::termpos pos_ = 0);
00133
00135 Query(Query::op op_, const Query & left, const Query & right);
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00147 Query(Query::op op_,
00148 const std::string & left, const std::string & right);
00149
00163 template <class Iterator>
00164 Query(Query::op op_, Iterator qbegin, Iterator qend);
00165
00171 template <class SubQ> Query(Query::op op_, SubQ q);
00172
00175 void set_window(Xapian::termpos window);
00176
00179 void set_cutoff(Xapian::weight cutoff);
00180
00182 void set_elite_set_size(Xapian::termcount size);
00183
00188 Xapian::termcount get_length() const;
00189
00195 Xapian::termcount set_length(Xapian::termcount qlen);
00196
00202 TermIterator get_terms_begin() const;
00203
00207 TermIterator get_terms_end() const;
00208
00210
00211 bool is_empty() const;
00212
00216 std::string get_description() const;
00217
00218 private:
00219 void add_subquery(const Query & subq);
00220 void add_subquery(const Query * subq);
00221 void add_subquery(const std::string & tname);
00222 void start_construction(Query::op op_);
00223 void end_construction();
00224 void abort_construction();
00225 };
00226
00227 template <class Iterator>
00228 Query::Query(Query::op op_, Iterator qbegin, Iterator qend) : internal(0)
00229 {
00230 try {
00231 start_construction(op_);
00232
00233
00234 while (qbegin != qend) {
00235 add_subquery(*qbegin);
00236 ++qbegin;
00237 }
00238
00239 end_construction();
00240 } catch (...) {
00241 abort_construction();
00242 throw;
00243 }
00244 }
00245
00246 template <class SubQ>
00247 Query::Query(Query::op op_, SubQ q) : internal(0)
00248 {
00249 try {
00250 start_construction(op_);
00251 add_subquery(q);
00252 end_construction();
00253 } catch (...) {
00254 abort_construction();
00255 throw;
00256 }
00257 }
00258
00260 class Query::Internal : public Xapian::Internal::RefCntBase {
00261 friend class MultiMatch;
00262 friend class LocalSubMatch;
00263 friend class SortPosName;
00264 public:
00265 static const int OP_LEAF = -1;
00266
00268 typedef std::vector<Internal *> subquery_list;
00269
00271 typedef int op_t;
00272
00273 private:
00275 op_t op;
00276
00278 subquery_list subqs;
00279
00281 Xapian::termcount qlen;
00282
00286 Xapian::termpos window;
00287
00290 double cutoff;
00291
00294 Xapian::termcount elite_set_size;
00295
00297 std::string tname;
00298
00300 Xapian::termpos term_pos;
00301
00303 Xapian::termcount wqf;
00304
00312 void swap(Query::Internal &other);
00313
00315 void initialise_from_copy(const Query::Internal & copyme);
00316
00317 void accumulate_terms(
00318 std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00319
00324 Internal * simplify_query();
00325
00330 void prevalidate_query() const;
00331
00339 void validate_query() const;
00340
00343 static std::string get_op_name(Xapian::Query::Internal::op_t op);
00344
00347 void collapse_subqs();
00348
00352 void flatten_subqs();
00353
00354 public:
00356 Internal(const Query::Internal & copyme);
00357
00359 void operator=(const Query::Internal & copyme);
00360
00362 Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00363 Xapian::termpos term_pos_ = 0);
00364
00366 Internal(op_t op_);
00367
00369 ~Internal();
00370
00371 static Xapian::Query::Internal * unserialise(const std::string &s);
00372
00375 void add_subquery(const Query::Internal & subq);
00376
00379 Query::Internal * end_construction();
00380
00384 std::string serialise() const;
00385
00389 std::string get_description() const;
00390
00392 void set_window(Xapian::termpos window);
00393
00395 void set_cutoff(double cutoff);
00396
00398 void set_elite_set_size(Xapian::termcount size);
00399
00404 Xapian::termcount get_length() const { return qlen; }
00405
00411 Xapian::termcount set_length(Xapian::termcount qlen_);
00412
00418 TermIterator get_terms() const;
00419 };
00420
00421 }
00422
00423 #endif