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
00029 #include <xapian/base.h>
00030 #include <xapian/deprecated.h>
00031 #include <xapian/sorter.h>
00032 #include <xapian/types.h>
00033 #include <xapian/termiterator.h>
00034 #include <xapian/visibility.h>
00035
00036 namespace Xapian {
00037
00038 class Database;
00039 class Document;
00040 class ErrorHandler;
00041 class ExpandDecider;
00042 class MSetIterator;
00043 class Query;
00044 class Weight;
00045
00049 class XAPIAN_VISIBILITY_DEFAULT MSet {
00050 public:
00051 class Internal;
00053 Xapian::Internal::RefCntPtr<Internal> internal;
00054
00056 explicit MSet(MSet::Internal * internal_);
00057
00059 MSet();
00060
00062 ~MSet();
00063
00065 MSet(const MSet & other);
00066
00068 void operator=(const MSet &other);
00069
00085 void fetch(const MSetIterator &begin, const MSetIterator &end) const;
00086
00089 void fetch(const MSetIterator &item) const;
00090
00093 void fetch() const;
00094
00099 Xapian::percent convert_to_percent(Xapian::weight wt) const;
00100
00102 Xapian::percent convert_to_percent(const MSetIterator &it) const;
00103
00111 Xapian::doccount get_termfreq(const std::string &tname) const;
00112
00120 Xapian::weight get_termweight(const std::string &tname) const;
00121
00129 Xapian::doccount get_firstitem() const;
00130
00140 Xapian::doccount get_matches_lower_bound() const;
00141
00154 Xapian::doccount get_matches_estimated() const;
00155
00165 Xapian::doccount get_matches_upper_bound() const;
00166
00173 Xapian::weight get_max_possible() const;
00174
00188 Xapian::weight get_max_attained() const;
00189
00191 Xapian::doccount size() const;
00192
00194 Xapian::doccount max_size() const { return size(); }
00195
00197 bool empty() const;
00198
00200 void swap(MSet & other);
00201
00203 MSetIterator begin() const;
00204
00206 MSetIterator end() const;
00207
00209 MSetIterator back() const;
00210
00220 MSetIterator operator[](Xapian::doccount i) const;
00221
00223
00224 typedef MSetIterator value_type;
00225 typedef MSetIterator iterator;
00226 typedef MSetIterator const_iterator;
00227 typedef MSetIterator & reference;
00228 typedef MSetIterator & const_reference;
00229 typedef MSetIterator * pointer;
00230 typedef Xapian::doccount_diff difference_type;
00231 typedef Xapian::doccount size_type;
00233
00235 std::string get_description() const;
00236 };
00237
00241 class XAPIAN_VISIBILITY_DEFAULT MSetIterator {
00242 private:
00243 friend class MSet;
00244 friend bool operator==(const MSetIterator &a, const MSetIterator &b);
00245 friend bool operator!=(const MSetIterator &a, const MSetIterator &b);
00246
00247 MSetIterator(Xapian::doccount index_, const MSet & mset_)
00248 : index(index_), mset(mset_) { }
00249
00250 Xapian::doccount index;
00251 MSet mset;
00252
00253 public:
00257 MSetIterator() : index(0), mset() { }
00258
00259 ~MSetIterator() { }
00260
00262 MSetIterator(const MSetIterator &other) {
00263 index = other.index;
00264 mset = other.mset;
00265 }
00266
00268 void operator=(const MSetIterator &other) {
00269 index = other.index;
00270 mset = other.mset;
00271 }
00272
00274 MSetIterator & operator++() {
00275 ++index;
00276 return *this;
00277 }
00278
00280 MSetIterator operator++(int) {
00281 MSetIterator tmp = *this;
00282 ++index;
00283 return tmp;
00284 }
00285
00287 MSetIterator & operator--() {
00288 --index;
00289 return *this;
00290 }
00291
00293 MSetIterator operator--(int) {
00294 MSetIterator tmp = *this;
00295 --index;
00296 return tmp;
00297 }
00298
00300 Xapian::docid operator*() const;
00301
00318 Xapian::Document get_document() const;
00319
00326 Xapian::doccount get_rank() const {
00327 return mset.get_firstitem() + index;
00328 }
00329
00331 Xapian::weight get_weight() const;
00332
00335 std::string get_collapse_key() const;
00336
00353 Xapian::doccount get_collapse_count() const;
00354
00370 Xapian::percent get_percent() const;
00371
00373 std::string get_description() const;
00374
00376
00377 typedef std::bidirectional_iterator_tag iterator_category;
00378 typedef Xapian::docid value_type;
00379 typedef Xapian::doccount_diff difference_type;
00380 typedef Xapian::docid * pointer;
00381 typedef Xapian::docid & reference;
00383 };
00384
00386 inline bool operator==(const MSetIterator &a, const MSetIterator &b)
00387 {
00388 return (a.index == b.index);
00389 }
00390
00392 inline bool operator!=(const MSetIterator &a, const MSetIterator &b)
00393 {
00394 return (a.index != b.index);
00395 }
00396
00397 class ESetIterator;
00398
00403 class XAPIAN_VISIBILITY_DEFAULT ESet {
00404 public:
00405 class Internal;
00407 Xapian::Internal::RefCntPtr<Internal> internal;
00408
00410 ESet();
00411
00413 ~ESet();
00414
00416 ESet(const ESet & other);
00417
00419 void operator=(const ESet &other);
00420
00425 Xapian::termcount get_ebound() const;
00426
00428 Xapian::termcount size() const;
00429
00431 Xapian::termcount max_size() const { return size(); }
00432
00434 bool empty() const;
00435
00437 void swap(ESet & other);
00438
00440 ESetIterator begin() const;
00441
00443 ESetIterator end() const;
00444
00446 ESetIterator back() const;
00447
00449 ESetIterator operator[](Xapian::termcount i) const;
00450
00452 std::string get_description() const;
00453 };
00454
00456 class XAPIAN_VISIBILITY_DEFAULT ESetIterator {
00457 private:
00458 friend class ESet;
00459 friend bool operator==(const ESetIterator &a, const ESetIterator &b);
00460 friend bool operator!=(const ESetIterator &a, const ESetIterator &b);
00461
00462 ESetIterator(Xapian::termcount index_, const ESet & eset_)
00463 : index(index_), eset(eset_) { }
00464
00465 Xapian::termcount index;
00466 ESet eset;
00467
00468 public:
00472 ESetIterator() : index(0), eset() { }
00473
00474 ~ESetIterator() { }
00475
00477 ESetIterator(const ESetIterator &other) {
00478 index = other.index;
00479 eset = other.eset;
00480 }
00481
00483 void operator=(const ESetIterator &other) {
00484 index = other.index;
00485 eset = other.eset;
00486 }
00487
00489 ESetIterator & operator++() {
00490 ++index;
00491 return *this;
00492 }
00493
00495 ESetIterator operator++(int) {
00496 ESetIterator tmp = *this;
00497 ++index;
00498 return tmp;
00499 }
00500
00502 ESetIterator & operator--() {
00503 --index;
00504 return *this;
00505 }
00506
00508 ESetIterator operator--(int) {
00509 ESetIterator tmp = *this;
00510 --index;
00511 return tmp;
00512 }
00513
00515 const std::string & operator *() const;
00516
00518 Xapian::weight get_weight() const;
00519
00521 std::string get_description() const;
00522
00524
00525 typedef std::bidirectional_iterator_tag iterator_category;
00526 typedef std::string value_type;
00527 typedef Xapian::termcount_diff difference_type;
00528 typedef std::string * pointer;
00529 typedef std::string & reference;
00531 };
00532
00534 inline bool operator==(const ESetIterator &a, const ESetIterator &b)
00535 {
00536 return (a.index == b.index);
00537 }
00538
00540 inline bool operator!=(const ESetIterator &a, const ESetIterator &b)
00541 {
00542 return (a.index != b.index);
00543 }
00544
00549 class XAPIAN_VISIBILITY_DEFAULT RSet {
00550 public:
00552 class Internal;
00553
00555 Xapian::Internal::RefCntPtr<Internal> internal;
00556
00558 RSet(const RSet &rset);
00559
00561 void operator=(const RSet &rset);
00562
00564 RSet();
00565
00567 ~RSet();
00568
00570 Xapian::doccount size() const;
00571
00573 bool empty() const;
00574
00576 void add_document(Xapian::docid did);
00577
00579 void add_document(const Xapian::MSetIterator & i) { add_document(*i); }
00580
00582 void remove_document(Xapian::docid did);
00583
00585 void remove_document(const Xapian::MSetIterator & i) { remove_document(*i); }
00586
00588 bool contains(Xapian::docid did) const;
00589
00591 bool contains(const Xapian::MSetIterator & i) const { return contains(*i); }
00592
00594 std::string get_description() const;
00595 };
00596
00599 class XAPIAN_VISIBILITY_DEFAULT MatchDecider {
00600 public:
00606 virtual bool operator()(const Xapian::Document &doc) const = 0;
00607
00609 virtual ~MatchDecider();
00610 };
00611
00622 class XAPIAN_VISIBILITY_DEFAULT Enquire {
00623 public:
00625 Enquire(const Enquire & other);
00626
00628 void operator=(const Enquire & other);
00629
00630 class Internal;
00632 Xapian::Internal::RefCntPtr<Internal> internal;
00633
00658 explicit Enquire(const Database &database, ErrorHandler * errorhandler_ = 0);
00659
00662 ~Enquire();
00663
00670 void set_query(const Xapian::Query & query, Xapian::termcount qlen = 0);
00671
00678 const Xapian::Query & get_query() const;
00679
00686 void set_weighting_scheme(const Weight &weight_);
00687
00714 void set_collapse_key(Xapian::valueno collapse_key);
00715
00716 typedef enum {
00717 ASCENDING = 1,
00718 DESCENDING = 0,
00719 DONT_CARE = 2
00720 } docid_order;
00721
00745 void set_docid_order(docid_order order);
00746
00765 void set_cutoff(Xapian::percent percent_cutoff, Xapian::weight weight_cutoff = 0);
00766
00771 void set_sort_by_relevance();
00772
00788 void set_sort_by_value(Xapian::valueno sort_key, bool reverse = true);
00789
00798 void set_sort_by_key(Xapian::Sorter * sorter, bool reverse = true);
00799
00816 void set_sort_by_value_then_relevance(Xapian::valueno sort_key,
00817 bool reverse = true);
00818
00828 void set_sort_by_key_then_relevance(Xapian::Sorter * sorter,
00829 bool reverse = true);
00830
00853 void set_sort_by_relevance_then_value(Xapian::valueno sort_key,
00854 bool reverse = true);
00855
00872 void set_sort_by_relevance_then_key(Xapian::Sorter * sorter,
00873 bool reverse = true);
00874
00919 MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00920 Xapian::doccount checkatleast = 0,
00921 const RSet * omrset = 0,
00922 const MatchDecider * mdecider = 0) const;
00923 MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00924 Xapian::doccount checkatleast,
00925 const RSet * omrset,
00926 const MatchDecider * mdecider,
00927 const MatchDecider * matchspy) const;
00928 MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00929 const RSet * omrset,
00930 const MatchDecider * mdecider = 0) const {
00931 return get_mset(first, maxitems, 0, omrset, mdecider);
00932 }
00933
00934 static const int INCLUDE_QUERY_TERMS = 1;
00935 static const int USE_EXACT_TERMFREQ = 2;
00936 #ifndef _MSC_VER
00938 XAPIAN_DEPRECATED(static const int include_query_terms) = 1;
00940 XAPIAN_DEPRECATED(static const int use_exact_termfreq) = 2;
00941 #else
00942
00943
00944 static const int include_query_terms = 1;
00945 static const int use_exact_termfreq = 2;
00946 #pragma deprecated("Xapian::Enquire::include_query_terms", "Xapian::Enquire::use_exact_termfreq")
00947 #endif
00948
00971 ESet get_eset(Xapian::termcount maxitems,
00972 const RSet & omrset,
00973 int flags = 0,
00974 double k = 1.0,
00975 const Xapian::ExpandDecider * edecider = 0) const;
00976
00990 inline ESet get_eset(Xapian::termcount maxitems, const RSet & omrset,
00991 const Xapian::ExpandDecider * edecider) const {
00992 return get_eset(maxitems, omrset, 0, 1.0, edecider);
00993 }
00994
01023 TermIterator get_matching_terms_begin(Xapian::docid did) const;
01024
01026 TermIterator get_matching_terms_end(Xapian::docid ) const {
01027 return TermIterator(NULL);
01028 }
01029
01052 TermIterator get_matching_terms_begin(const MSetIterator &it) const;
01053
01055 TermIterator get_matching_terms_end(const MSetIterator &) const {
01056 return TermIterator(NULL);
01057 }
01058
01071 XAPIAN_DEPRECATED(
01072 void register_match_decider(const std::string &name,
01073 const MatchDecider *mdecider = NULL));
01074
01076 std::string get_description() const;
01077 };
01078
01079 }
01080
01081 class RemoteServer;
01082 class ScaleWeight;
01083
01084 namespace Xapian {
01085
01087 class XAPIAN_VISIBILITY_DEFAULT Weight {
01088 friend class Enquire;
01089 friend class ::RemoteServer;
01090 friend class ::ScaleWeight;
01091 public:
01092 class Internal;
01093 protected:
01094 Weight(const Weight &);
01095 private:
01096 void operator=(Weight &);
01097
01107 virtual Weight * clone() const = 0;
01108
01109 protected:
01110 const Internal * internal;
01111 Xapian::doclength querysize;
01112 Xapian::termcount wqf;
01113 std::string tname;
01114
01115 public:
01116
01117 Weight() { }
01118 virtual ~Weight();
01119
01132 Weight * create(const Internal * internal_, Xapian::doclength querysize_,
01133 Xapian::termcount wqf_, const std::string & tname_) const;
01134
01139 virtual std::string name() const = 0;
01140
01142 virtual std::string serialise() const = 0;
01143
01145 virtual Weight * unserialise(const std::string &s) const = 0;
01146
01154 virtual Xapian::weight get_sumpart(Xapian::termcount wdf,
01155 Xapian::doclength len) const = 0;
01156
01162 virtual Xapian::weight get_maxpart() const = 0;
01163
01172 virtual Xapian::weight get_sumextra(Xapian::doclength len) const = 0;
01173
01177 virtual Xapian::weight get_maxextra() const = 0;
01178
01180 virtual bool get_sumpart_needs_doclength() const;
01181 };
01182
01184 class XAPIAN_VISIBILITY_DEFAULT BoolWeight : public Weight {
01185 public:
01186 BoolWeight * clone() const;
01187 BoolWeight() { }
01188 ~BoolWeight();
01189 std::string name() const;
01190 std::string serialise() const;
01191 BoolWeight * unserialise(const std::string & s) const;
01192 Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01193 Xapian::weight get_maxpart() const;
01194
01195 Xapian::weight get_sumextra(Xapian::doclength len) const;
01196 Xapian::weight get_maxextra() const;
01197
01198 bool get_sumpart_needs_doclength() const;
01199 };
01200
01213 class XAPIAN_VISIBILITY_DEFAULT BM25Weight : public Weight {
01214 private:
01215 mutable Xapian::weight termweight;
01216 mutable Xapian::doclength lenpart;
01217
01218 double k1, k2, k3, b;
01219 Xapian::doclength min_normlen;
01220
01221 mutable bool weight_calculated;
01222
01223 void calc_termweight() const;
01224
01225 public:
01244 BM25Weight(double k1_, double k2_, double k3_, double b_,
01245 double min_normlen_)
01246 : k1(k1_), k2(k2_), k3(k3_), b(b_), min_normlen(min_normlen_),
01247 weight_calculated(false)
01248 {
01249 if (k1 < 0) k1 = 0;
01250 if (k2 < 0) k2 = 0;
01251 if (k3 < 0) k3 = 0;
01252 if (b < 0) b = 0; else if (b > 1) b = 1;
01253 }
01254 BM25Weight() : k1(1), k2(0), k3(1), b(0.5), min_normlen(0.5),
01255 weight_calculated(false) { }
01256
01257 BM25Weight * clone() const;
01258 ~BM25Weight() { }
01259 std::string name() const;
01260 std::string serialise() const;
01261 BM25Weight * unserialise(const std::string & s) const;
01262 Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01263 Xapian::weight get_maxpart() const;
01264
01265 Xapian::weight get_sumextra(Xapian::doclength len) const;
01266 Xapian::weight get_maxextra() const;
01267
01268 bool get_sumpart_needs_doclength() const;
01269 };
01270
01288 class XAPIAN_VISIBILITY_DEFAULT TradWeight : public Weight {
01289 private:
01290 mutable Xapian::weight termweight;
01291 mutable Xapian::doclength lenpart;
01292
01293 double param_k;
01294
01295 mutable bool weight_calculated;
01296
01297 void calc_termweight() const;
01298
01299 public:
01307 explicit TradWeight(double k) : param_k(k), weight_calculated(false) {
01308 if (param_k < 0) param_k = 0;
01309 }
01310
01311 TradWeight() : param_k(1.0), weight_calculated(false) { }
01312
01313 TradWeight * clone() const;
01314 ~TradWeight() { }
01315 std::string name() const;
01316 std::string serialise() const;
01317 TradWeight * unserialise(const std::string & s) const;
01318
01319 Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01320 Xapian::weight get_maxpart() const;
01321
01322 Xapian::weight get_sumextra(Xapian::doclength len) const;
01323 Xapian::weight get_maxextra() const;
01324
01325 bool get_sumpart_needs_doclength() const;
01326 };
01327
01328 }
01329
01330 #endif