00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef XAPIAN_INCLUDED_ENQUIRE_H
00025 #define XAPIAN_INCLUDED_ENQUIRE_H
00026
00027 #include <string>
00028 #include <time.h>
00029
00030 #include <xapian/base.h>
00031 #include <xapian/error.h>
00032 #include <xapian/types.h>
00033 #include <xapian/termiterator.h>
00034
00035 namespace Xapian {
00036
00037 class Database;
00038 class Document;
00039 class ErrorHandler;
00040 class MSetIterator;
00041 class Query;
00042 class Weight;
00043
00047 class MSet {
00048 public:
00049 class Internal;
00051 Xapian::Internal::RefCntPtr<Internal> internal;
00052
00054 explicit MSet(MSet::Internal * internal_);
00055
00057 MSet();
00058
00060 ~MSet();
00061
00063 MSet(const MSet & other);
00064
00066 void operator=(const MSet &other);
00067
00083 void fetch(const MSetIterator &begin, const MSetIterator &end) const;
00084
00087 void fetch(const MSetIterator &item) const;
00088
00091 void fetch() const;
00092
00097 Xapian::percent convert_to_percent(Xapian::weight wt) const;
00098
00100 Xapian::percent convert_to_percent(const MSetIterator &it) const;
00101
00109 Xapian::doccount get_termfreq(const std::string &tname) const;
00110
00118 Xapian::weight get_termweight(const std::string &tname) const;
00119
00127 Xapian::doccount get_firstitem() const;
00128
00138 Xapian::doccount get_matches_lower_bound() const;
00139
00152 Xapian::doccount get_matches_estimated() const;
00153
00163 Xapian::doccount get_matches_upper_bound() const;
00164
00170 Xapian::weight get_max_possible() const;
00171
00185 Xapian::weight get_max_attained() const;
00186
00188 Xapian::doccount size() const;
00189
00191 Xapian::doccount max_size() const { return size(); }
00192
00194 bool empty() const;
00195
00197 void swap(MSet & other);
00198
00200 MSetIterator begin() const;
00201
00203 MSetIterator end() const;
00204
00206 MSetIterator back() const;
00207
00217 MSetIterator operator[](Xapian::doccount i) const;
00218
00220
00221 typedef MSetIterator value_type;
00222 typedef MSetIterator iterator;
00223 typedef MSetIterator const_iterator;
00224 typedef MSetIterator & reference;
00225 typedef MSetIterator & const_reference;
00226 typedef MSetIterator * pointer;
00227 typedef Xapian::doccount_diff difference_type;
00228 typedef Xapian::doccount size_type;
00230
00234 std::string get_description() const;
00235 };
00236
00240 class MSetIterator {
00241 private:
00242 friend class MSet;
00243 friend bool operator==(const MSetIterator &a, const MSetIterator &b);
00244 friend bool operator!=(const MSetIterator &a, const MSetIterator &b);
00245
00246 MSetIterator(Xapian::doccount index_, const MSet & mset_)
00247 : index(index_), mset(mset_) { }
00248
00249 Xapian::doccount index;
00250 MSet mset;
00251
00252 public:
00256 MSetIterator() : index(0), mset() { }
00257
00258 ~MSetIterator() { }
00259
00261 MSetIterator(const MSetIterator &other) {
00262 index = other.index;
00263 mset = other.mset;
00264 }
00265
00267 void operator=(const MSetIterator &other) {
00268 index = other.index;
00269 mset = other.mset;
00270 }
00271
00273 MSetIterator & operator++() {
00274 ++index;
00275 return *this;
00276 }
00277
00279 MSetIterator operator++(int) {
00280 MSetIterator tmp = *this;
00281 ++index;
00282 return tmp;
00283 }
00284
00286 MSetIterator & operator--() {
00287 --index;
00288 return *this;
00289 }
00290
00292 MSetIterator operator--(int) {
00293 MSetIterator tmp = *this;
00294 --index;
00295 return tmp;
00296 }
00297
00299 Xapian::docid operator*() const;
00300
00317 Xapian::Document get_document() const;
00318
00325 Xapian::doccount get_rank() const {
00326 return mset.get_firstitem() + index;
00327 }
00328
00330 Xapian::weight get_weight() const;
00331
00334 std::string get_collapse_key() const;
00335
00352 Xapian::doccount get_collapse_count() const;
00353
00359 Xapian::percent get_percent() const;
00360
00364 std::string get_description() const;
00365
00367
00368 typedef std::bidirectional_iterator_tag iterator_category;
00369 typedef Xapian::docid value_type;
00370 typedef Xapian::doccount_diff difference_type;
00371 typedef Xapian::docid * pointer;
00372 typedef Xapian::docid & reference;
00374 };
00375
00376 inline bool operator==(const MSetIterator &a, const MSetIterator &b)
00377 {
00378 return (a.index == b.index);
00379 }
00380
00381 inline bool operator!=(const MSetIterator &a, const MSetIterator &b)
00382 {
00383 return (a.index != b.index);
00384 }
00385
00386 class ESetIterator;
00387
00392 class ESet {
00393 public:
00394 class Internal;
00396 Xapian::Internal::RefCntPtr<Internal> internal;
00397
00399 ESet();
00400
00402 ~ESet();
00403
00405 ESet(const ESet & other);
00406
00408 void operator=(const ESet &other);
00409
00414 Xapian::termcount get_ebound() const;
00415
00417 Xapian::termcount size() const;
00418
00420 Xapian::termcount max_size() const { return size(); }
00421
00423 bool empty() const;
00424
00426 void swap(ESet & other);
00427
00429 ESetIterator begin() const;
00430
00432 ESetIterator end() const;
00433
00435 ESetIterator back() const;
00436
00438 ESetIterator operator[](Xapian::termcount i) const;
00439
00444 std::string get_description() const;
00445 };
00446
00448 class ESetIterator {
00449 private:
00450 friend class ESet;
00451 friend bool operator==(const ESetIterator &a, const ESetIterator &b);
00452 friend bool operator!=(const ESetIterator &a, const ESetIterator &b);
00453
00454 ESetIterator(Xapian::termcount index_, const ESet & eset_)
00455 : index(index_), eset(eset_) { }
00456
00457 Xapian::termcount index;
00458 ESet eset;
00459
00460 public:
00464 ESetIterator() : index(0), eset() { }
00465
00466 ~ESetIterator() { }
00467
00469 ESetIterator(const ESetIterator &other) {
00470 index = other.index;
00471 eset = other.eset;
00472 }
00473
00475 void operator=(const ESetIterator &other) {
00476 index = other.index;
00477 eset = other.eset;
00478 }
00479
00481 ESetIterator & operator++() {
00482 ++index;
00483 return *this;
00484 }
00485
00487 ESetIterator operator++(int) {
00488 ESetIterator tmp = *this;
00489 ++index;
00490 return tmp;
00491 }
00492
00494 ESetIterator & operator--() {
00495 --index;
00496 return *this;
00497 }
00498
00500 ESetIterator operator--(int) {
00501 ESetIterator tmp = *this;
00502 --index;
00503 return tmp;
00504 }
00505
00507 const std::string & operator *() const;
00508
00510 Xapian::weight get_weight() const;
00511
00515 std::string get_description() const;
00516
00518
00519 typedef std::bidirectional_iterator_tag iterator_category;
00520 typedef std::string value_type;
00521 typedef Xapian::termcount_diff difference_type;
00522 typedef std::string * pointer;
00523 typedef std::string & reference;
00525 };
00526
00527 inline bool operator==(const ESetIterator &a, const ESetIterator &b)
00528 {
00529 return (a.index == b.index);
00530 }
00531
00532 inline bool operator!=(const ESetIterator &a, const ESetIterator &b)
00533 {
00534 return (a.index != b.index);
00535 }
00536
00541 class RSet {
00542 public:
00544 class Internal;
00545
00547 Xapian::Internal::RefCntPtr<Internal> internal;
00548
00550 RSet(const RSet &rset);
00551
00553 void operator=(const RSet &rset);
00554
00556 RSet();
00557
00559 ~RSet();
00560
00562 Xapian::doccount size() const;
00563
00565 bool empty() const;
00566
00568 void add_document(Xapian::docid did);
00569
00571 void add_document(const Xapian::MSetIterator & i) { add_document(*i); }
00572
00574 void remove_document(Xapian::docid did);
00575
00577 void remove_document(const Xapian::MSetIterator & i) { remove_document(*i); }
00578
00580 bool contains(Xapian::docid did) const;
00581
00583 bool contains(const Xapian::MSetIterator & i) { return contains(*i); }
00584
00589 std::string get_description() const;
00590 };
00591
00594 class MatchDecider {
00595 public:
00598 virtual int operator()(const Xapian::Document &doc) const = 0;
00599
00601 virtual ~MatchDecider() {}
00602 };
00603
00606 class ExpandDecider {
00607 public:
00610 virtual int operator()(const std::string & tname) const = 0;
00611
00613 virtual ~ExpandDecider() {}
00614 };
00615
00626 class Enquire {
00627 private:
00629 Enquire(const Enquire &);
00630
00632 void operator=(const Enquire &);
00633
00634 public:
00635 class Internal;
00637 Xapian::Internal::RefCntPtr<Internal> internal;
00638
00654 Enquire(const Database &databases, ErrorHandler * errorhandler_ = 0);
00655
00658 ~Enquire();
00659
00666 void set_query(const Xapian::Query & query, Xapian::termcount qlen = 0);
00667
00674 const Xapian::Query & get_query();
00675
00682 void set_weighting_scheme(const Weight &weight_);
00683
00710 void set_collapse_key(Xapian::valueno collapse_key);
00711
00712 typedef enum {
00713 ASCENDING = 1,
00714 DESCENDING = 0,
00715 DONT_CARE = 2
00716 } docid_order;
00717
00741 void set_docid_order(docid_order order);
00742
00749 XAPIAN_DEPRECATED(void set_sort_forward(bool sort_forward));
00750
00769 void set_cutoff(Xapian::percent percent_cutoff, Xapian::weight weight_cutoff = 0);
00770
00787 XAPIAN_DEPRECATED(void set_sorting(Xapian::valueno sort_key, int sort_bands,
00788 bool sort_by_relevance = false));
00789
00794 void set_sort_by_relevance();
00795
00806 void set_sort_by_value(Xapian::valueno sort_key, bool ascending = true);
00807
00819 void set_sort_by_value_then_relevance(Xapian::valueno sort_key,
00820 bool ascending = true);
00821
00839 void set_sort_by_relevance_then_value(Xapian::valueno sort_key,
00840 bool ascending = true);
00841
00853 void set_bias(Xapian::weight bias_weight, time_t bias_halflife);
00854
00880 MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00881 Xapian::doccount checkatleast = 0,
00882 const RSet * omrset = 0,
00883 const MatchDecider * mdecider = 0) const;
00884 MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00885 const RSet * omrset,
00886 const MatchDecider * mdecider = 0) const {
00887 return get_mset(first, maxitems, 0, omrset, mdecider);
00888 }
00889
00890 static const int include_query_terms = 1;
00891 static const int use_exact_termfreq = 2;
00914 ESet get_eset(Xapian::termcount maxitems,
00915 const RSet & omrset,
00916 int flags = 0,
00917 double k = 1.0,
00918 const Xapian::ExpandDecider * edecider = 0) const;
00919
00933 inline ESet get_eset(Xapian::termcount maxitems, const RSet & omrset,
00934 const Xapian::ExpandDecider * edecider) const {
00935 return get_eset(maxitems, omrset, 0, 1.0, edecider);
00936 }
00937
00966 TermIterator get_matching_terms_begin(Xapian::docid did) const;
00967
00969 TermIterator get_matching_terms_end(Xapian::docid ) const {
00970 return TermIterator(NULL);
00971 }
00972
00995 TermIterator get_matching_terms_begin(const MSetIterator &it) const;
00996
00998 TermIterator get_matching_terms_end(const MSetIterator &) const {
00999 return TermIterator(NULL);
01000 }
01001
01008 void register_match_decider(const std::string &name,
01009 const MatchDecider *mdecider = NULL);
01010
01014 std::string get_description() const;
01015 };
01016
01017 }
01018
01019 class RemoteServer;
01020
01021 namespace Xapian {
01022
01024 class Weight {
01025 friend class Enquire;
01026 friend class ::RemoteServer;
01027 public:
01028 class Internal;
01029 protected:
01030 Weight(const Weight &);
01031 private:
01032 void operator=(Weight &);
01033
01043 virtual Weight * clone() const = 0;
01044
01045 protected:
01046 const Internal * internal;
01047 Xapian::doclength querysize;
01048 Xapian::termcount wqf;
01049 std::string tname;
01050
01051 public:
01052 Weight() { }
01053 virtual ~Weight() { }
01054
01067 Weight * create(const Internal * internal_, Xapian::doclength querysize_,
01068 Xapian::termcount wqf_, std::string tname_) const {
01069 Weight * wt = clone();
01070 wt->internal = internal_;
01071 wt->querysize = querysize_;
01072 wt->wqf = wqf_;
01073 wt->tname = tname_;
01074 return wt;
01075 }
01076
01081 virtual std::string name() const = 0;
01082
01084 virtual std::string serialise() const = 0;
01085
01087 virtual Weight * unserialise(const std::string &s) const = 0;
01088
01096 virtual Xapian::weight get_sumpart(Xapian::termcount wdf,
01097 Xapian::doclength len) const = 0;
01098
01104 virtual Xapian::weight get_maxpart() const = 0;
01105
01114 virtual Xapian::weight get_sumextra(Xapian::doclength len) const = 0;
01115
01119 virtual Xapian::weight get_maxextra() const = 0;
01120
01122 virtual bool get_sumpart_needs_doclength() const { return true; }
01123 };
01124
01126 class BoolWeight : public Weight {
01127 public:
01128 BoolWeight * clone() const {
01129 return new BoolWeight;
01130 }
01131 BoolWeight() { }
01132 ~BoolWeight() { }
01133 std::string name() const { return "Bool"; }
01134 std::string serialise() const { return ""; }
01135 BoolWeight * unserialise(const std::string & ) const {
01136 return new BoolWeight;
01137 }
01138 Xapian::weight get_sumpart(Xapian::termcount , Xapian::doclength ) const { return 0; }
01139 Xapian::weight get_maxpart() const { return 0; }
01140
01141 Xapian::weight get_sumextra(Xapian::doclength ) const { return 0; }
01142 Xapian::weight get_maxextra() const { return 0; }
01143
01144 bool get_sumpart_needs_doclength() const { return false; }
01145 };
01146
01159 class BM25Weight : public Weight {
01160 private:
01161 mutable Xapian::weight termweight;
01162 mutable Xapian::doclength lenpart;
01163
01164 double k1, k2, k3, b;
01165 Xapian::doclength min_normlen;
01166
01167 mutable bool weight_calculated;
01168
01169 void calc_termweight() const;
01170
01171 public:
01190 BM25Weight(double k1_, double k2_, double k3_, double b_,
01191 double min_normlen_)
01192 : k1(k1_), k2(k2_), k3(k3_), b(b_), min_normlen(min_normlen_),
01193 weight_calculated(false)
01194 {
01195 if (k1 < 0) k1 = 0;
01196 if (k2 < 0) k2 = 0;
01197 if (k3 < 0) k3 = 0;
01198 if (b < 0) b = 0; else if (b > 1) b = 1;
01199 }
01200 BM25Weight() : k1(1), k2(0), k3(1), b(0.5), min_normlen(0.5),
01201 weight_calculated(false) { }
01202
01203 BM25Weight * clone() const;
01204 ~BM25Weight() { }
01205 std::string name() const;
01206 std::string serialise() const;
01207 BM25Weight * unserialise(const std::string & s) const;
01208 Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01209 Xapian::weight get_maxpart() const;
01210
01211 Xapian::weight get_sumextra(Xapian::doclength len) const;
01212 Xapian::weight get_maxextra() const;
01213
01214 bool get_sumpart_needs_doclength() const;
01215 };
01216
01234 class TradWeight : public Weight {
01235 private:
01236 mutable Xapian::weight termweight;
01237 mutable Xapian::doclength lenpart;
01238
01239 double param_k;
01240
01241 mutable bool weight_calculated;
01242
01243 void calc_termweight() const;
01244
01245 public:
01253 explicit TradWeight(double k) : param_k(k), weight_calculated(false) {
01254 if (param_k < 0) param_k = 0;
01255 }
01256
01257 TradWeight() : param_k(1.0), weight_calculated(false) { }
01258
01259 TradWeight * clone() const;
01260 ~TradWeight() { }
01261 std::string name() const;
01262 std::string serialise() const;
01263 TradWeight * unserialise(const std::string & s) const;
01264
01265 Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01266 Xapian::weight get_maxpart() const;
01267
01268 Xapian::weight get_sumextra(Xapian::doclength len) const;
01269 Xapian::weight get_maxextra() const;
01270
01271 bool get_sumpart_needs_doclength() const;
01272 };
01273
01274 }
01275
01276 #endif