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 #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
00047 class PostingSource;
00048 class Registry;
00049
00054 class XAPIAN_VISIBILITY_DEFAULT Query {
00055 public:
00057 class Internal;
00059 Xapian::Internal::RefCntPtr<Internal> internal;
00060
00062 typedef enum {
00064 OP_AND,
00065
00067 OP_OR,
00068
00070 OP_AND_NOT,
00071
00073 OP_XOR,
00074
00076 OP_AND_MAYBE,
00077
00079 OP_FILTER,
00080
00090 OP_NEAR,
00091
00101 OP_PHRASE,
00102
00104 OP_VALUE_RANGE,
00105
00114 OP_SCALE_WEIGHT,
00115
00157 OP_ELITE_SET,
00158
00160 OP_VALUE_GE,
00161
00163 OP_VALUE_LE,
00164
00180 OP_SYNONYM
00181 } op;
00182
00184 Query(const Query & copyme);
00185
00187 Query & operator=(const Query & copyme);
00188
00197 Query();
00198
00200 ~Query();
00201
00203 Query(const std::string & tname_, Xapian::termcount wqf_ = 1,
00204 Xapian::termpos pos_ = 0);
00205
00207 Query(Query::op op_, const Query & left, const Query & right);
00208
00210 Query(Query::op op_,
00211 const std::string & left, const std::string & right);
00212
00228 template <class Iterator>
00229 Query(Query::op op_, Iterator qbegin, Iterator qend,
00230 Xapian::termcount parameter = 0);
00231
00235 Query(Query::op op_, Xapian::Query q, double parameter);
00236
00250 Query(Query::op op_, Xapian::valueno slot,
00251 const std::string &begin, const std::string &end);
00252
00264 Query(Query::op op_, Xapian::valueno slot, const std::string &value);
00265
00276 explicit Query(Xapian::PostingSource * external_source);
00277
00279 static const Xapian::Query MatchAll;
00280
00282 static const Xapian::Query MatchNothing;
00283
00288 Xapian::termcount get_length() const;
00289
00295 TermIterator get_terms_begin() const;
00296
00300 TermIterator get_terms_end() const {
00301 return TermIterator();
00302 }
00303
00307 bool empty() const;
00308
00316 std::string serialise() const;
00317
00325 static Query unserialise(const std::string &s);
00326
00337 static Query unserialise(const std::string & s,
00338 const Registry & registry);
00339
00341 std::string get_description() const;
00342
00343 private:
00344 void add_subquery(const Query & subq);
00345 void add_subquery(const Query * subq);
00346 void add_subquery(const std::string & tname);
00347 void start_construction(Query::op op_, Xapian::termcount parameter);
00348 void end_construction();
00349 void abort_construction();
00350 };
00351
00352 template <class Iterator>
00353 Query::Query(Query::op op_, Iterator qbegin, Iterator qend, termcount parameter)
00354 : internal(0)
00355 {
00356 try {
00357 start_construction(op_, parameter);
00358
00359
00360 while (qbegin != qend) {
00361 add_subquery(*qbegin);
00362 ++qbegin;
00363 }
00364
00365 end_construction();
00366 } catch (...) {
00367 abort_construction();
00368 throw;
00369 }
00370 }
00371
00372 #ifndef SWIG // SWIG has no interest in the internal class, so hide it completely.
00373
00375 class XAPIAN_VISIBILITY_DEFAULT Query::Internal : public Xapian::Internal::RefCntBase {
00376 friend class ::LocalSubMatch;
00377 friend class ::MultiMatch;
00378 friend class ::QueryOptimiser;
00379 friend struct ::SortPosName;
00380 friend class Query;
00381 public:
00382 static const int OP_LEAF = -1;
00383 static const int OP_EXTERNAL_SOURCE = -2;
00384
00386 typedef std::vector<Internal *> subquery_list;
00387
00389 typedef int op_t;
00390
00391 private:
00393 Xapian::Query::Internal::op_t op;
00394
00396 subquery_list subqs;
00397
00407 Xapian::termcount parameter;
00408
00415 std::string tname;
00416
00418 std::string str_parameter;
00419
00421 Xapian::termpos term_pos;
00422
00424 Xapian::PostingSource * external_source;
00425
00427 bool external_source_owned;
00428
00430 void initialise_from_copy(const Query::Internal & copyme);
00431
00432 void accumulate_terms(
00433 std::vector<std::pair<std::string, Xapian::termpos> > &terms) const;
00434
00439 Internal * simplify_query();
00440
00446 void validate_query() const;
00447
00453 bool simplify_matchnothing();
00454
00457 static std::string get_op_name(Xapian::Query::Internal::op_t op);
00458
00461 void collapse_subqs();
00462
00466 Xapian::Query::Internal * flatten_subqs();
00467
00470 std::string serialise(Xapian::termpos & curpos) const;
00471
00472 public:
00474 Internal(const Query::Internal & copyme);
00475
00477 void operator=(const Query::Internal & copyme);
00478
00480 explicit Internal(const std::string & tname_, Xapian::termcount wqf_ = 1,
00481 Xapian::termpos term_pos_ = 0);
00482
00484 Internal(op_t op_, Xapian::termcount parameter);
00485
00487 Internal(op_t op_, Xapian::valueno slot,
00488 const std::string &begin, const std::string &end);
00489
00492 Internal(op_t op_, Xapian::valueno slot, const std::string &value);
00493
00495 explicit Internal(Xapian::PostingSource * external_source_, bool owned);
00496
00498 ~Internal();
00499
00500 static Xapian::Query::Internal * unserialise(const std::string &s,
00501 const Registry & registry);
00502
00504 void add_subquery(const Query::Internal * subq);
00505
00510 void add_subquery_nocopy(Query::Internal * subq);
00511
00512 void set_dbl_parameter(double dbl_parameter_);
00513
00514 double get_dbl_parameter() const;
00515
00518 Query::Internal * end_construction();
00519
00523 std::string serialise() const {
00524 Xapian::termpos curpos = 1;
00525 return serialise(curpos);
00526 }
00527
00529 std::string get_description() const;
00530
00538 Xapian::termcount get_parameter() const { return parameter; }
00539
00540 Xapian::termcount get_wqf() const { return parameter; }
00541
00546 Xapian::termcount get_length() const;
00547
00553 TermIterator get_terms() const;
00554 };
00555
00556 #endif // SWIG
00557
00558 }
00559
00560 #endif