include/xapian/enquire.h

Go to the documentation of this file.
00001 
00004 /* Copyright 1999,2000,2001 BrightStation PLC
00005  * Copyright 2001,2002 Ananova Ltd
00006  * Copyright 2002,2003,2004,2005,2006,2007 Olly Betts
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as
00010  * published by the Free Software Foundation; either version 2 of the
00011  * License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00021  * USA
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/types.h>
00032 #include <xapian/termiterator.h>
00033 #include <xapian/visibility.h>
00034 
00035 namespace Xapian {
00036 
00037 class Database;
00038 class Document;
00039 class ErrorHandler;
00040 class ExpandDecider;
00041 class MSetIterator;
00042 class Query;
00043 class Weight;
00044 
00048 class XAPIAN_VISIBILITY_DEFAULT MSet {
00049     public:
00050         class Internal;
00052         Xapian::Internal::RefCntPtr<Internal> internal;
00053 
00055         explicit MSet(MSet::Internal * internal_);
00056 
00058         MSet();
00059 
00061         ~MSet();
00062 
00064         MSet(const MSet & other);
00065 
00067         void operator=(const MSet &other);
00068 
00084         void fetch(const MSetIterator &begin, const MSetIterator &end) const;
00085 
00088         void fetch(const MSetIterator &item) const;
00089 
00092         void fetch() const;
00093 
00098         Xapian::percent convert_to_percent(Xapian::weight wt) const;
00099 
00101         Xapian::percent convert_to_percent(const MSetIterator &it) const;
00102 
00110         Xapian::doccount get_termfreq(const std::string &tname) const;
00111 
00119         Xapian::weight get_termweight(const std::string &tname) const;
00120 
00128         Xapian::doccount get_firstitem() const;
00129 
00139         Xapian::doccount get_matches_lower_bound() const;
00140 
00153         Xapian::doccount get_matches_estimated() const;
00154 
00164         Xapian::doccount get_matches_upper_bound() const;
00165 
00171         Xapian::weight get_max_possible() const;
00172 
00186         Xapian::weight get_max_attained() const;
00187 
00189         Xapian::doccount size() const;
00190 
00192         Xapian::doccount max_size() const { return size(); }
00193 
00195         bool empty() const;
00196 
00198         void swap(MSet & other);
00199 
00201         MSetIterator begin() const;
00202 
00204         MSetIterator end() const;
00205 
00207         MSetIterator back() const;
00208 
00218         MSetIterator operator[](Xapian::doccount i) const;
00219 
00221 
00222         typedef MSetIterator value_type; // FIXME: not assignable...
00223         typedef MSetIterator iterator;
00224         typedef MSetIterator const_iterator;
00225         typedef MSetIterator & reference; // Hmm
00226         typedef MSetIterator & const_reference;
00227         typedef MSetIterator * pointer; // Hmm
00228         typedef Xapian::doccount_diff difference_type;
00229         typedef Xapian::doccount size_type;
00231 
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 
00360         Xapian::percent get_percent() const;
00361 
00365         std::string get_description() const;
00366 
00368 
00369         typedef std::bidirectional_iterator_tag iterator_category; // FIXME: could enhance to be a randomaccess_iterator
00370         typedef Xapian::docid value_type;
00371         typedef Xapian::doccount_diff difference_type;
00372         typedef Xapian::docid * pointer;
00373         typedef Xapian::docid & reference;
00375 };
00376 
00377 inline bool operator==(const MSetIterator &a, const MSetIterator &b)
00378 {
00379     return (a.index == b.index);
00380 }
00381 
00382 inline bool operator!=(const MSetIterator &a, const MSetIterator &b)
00383 {
00384     return (a.index != b.index);
00385 }
00386 
00387 class ESetIterator;
00388 
00393 class XAPIAN_VISIBILITY_DEFAULT ESet {
00394     public:
00395         class Internal;
00397         Xapian::Internal::RefCntPtr<Internal> internal;
00398 
00400         ESet();
00401 
00403         ~ESet();
00404 
00406         ESet(const ESet & other);
00407 
00409         void operator=(const ESet &other);
00410 
00415         Xapian::termcount get_ebound() const;
00416 
00418         Xapian::termcount size() const;
00419 
00421         Xapian::termcount max_size() const { return size(); }
00422 
00424         bool empty() const;
00425 
00427         void swap(ESet & other);
00428 
00430         ESetIterator begin() const;
00431 
00433         ESetIterator end() const;
00434 
00436         ESetIterator back() const;
00437 
00439         ESetIterator operator[](Xapian::termcount i) const;
00440 
00445         std::string get_description() const;
00446 };
00447 
00449 class XAPIAN_VISIBILITY_DEFAULT ESetIterator {
00450     private:
00451         friend class ESet;
00452         friend bool operator==(const ESetIterator &a, const ESetIterator &b);
00453         friend bool operator!=(const ESetIterator &a, const ESetIterator &b);
00454 
00455         ESetIterator(Xapian::termcount index_, const ESet & eset_)
00456             : index(index_), eset(eset_) { }
00457 
00458         Xapian::termcount index;
00459         ESet eset;
00460 
00461     public:
00465         ESetIterator() : index(0), eset() { }
00466 
00467         ~ESetIterator() { }
00468 
00470         ESetIterator(const ESetIterator &other) {
00471             index = other.index;
00472             eset = other.eset;
00473         }
00474 
00476         void operator=(const ESetIterator &other) {
00477             index = other.index;
00478             eset = other.eset;
00479         }
00480 
00482         ESetIterator & operator++() {
00483             ++index;
00484             return *this;
00485         }
00486 
00488         ESetIterator operator++(int) {
00489             ESetIterator tmp = *this;
00490             ++index;
00491             return tmp;
00492         }
00493 
00495         ESetIterator & operator--() {
00496             --index;
00497             return *this;
00498         }
00499 
00501         ESetIterator operator--(int) {
00502             ESetIterator tmp = *this;
00503             --index;
00504             return tmp;
00505         }
00506 
00508         const std::string & operator *() const;
00509 
00511         Xapian::weight get_weight() const;
00512 
00516         std::string get_description() const;
00517 
00519 
00520         typedef std::bidirectional_iterator_tag iterator_category; // FIXME: go for randomaccess_iterator!
00521         typedef std::string value_type;
00522         typedef Xapian::termcount_diff difference_type;
00523         typedef std::string * pointer;
00524         typedef std::string & reference;
00526 };
00527 
00528 inline bool operator==(const ESetIterator &a, const ESetIterator &b)
00529 {
00530     return (a.index == b.index);
00531 }
00532 
00533 inline bool operator!=(const ESetIterator &a, const ESetIterator &b)
00534 {
00535     return (a.index != b.index);
00536 }
00537 
00542 class XAPIAN_VISIBILITY_DEFAULT RSet {
00543     public:
00545         class Internal;
00546 
00548         Xapian::Internal::RefCntPtr<Internal> internal;
00549 
00551         RSet(const RSet &rset);
00552 
00554         void operator=(const RSet &rset);
00555 
00557         RSet();
00558 
00560         ~RSet();
00561 
00563         Xapian::doccount size() const;
00564 
00566         bool empty() const;
00567 
00569         void add_document(Xapian::docid did);
00570 
00572         void add_document(const Xapian::MSetIterator & i) { add_document(*i); }
00573 
00575         void remove_document(Xapian::docid did);
00576 
00578         void remove_document(const Xapian::MSetIterator & i) { remove_document(*i); }
00579 
00581         bool contains(Xapian::docid did) const;
00582 
00584         bool contains(const Xapian::MSetIterator & i) const { return contains(*i); }
00585 
00590         std::string get_description() const;
00591 };
00592 
00595 class XAPIAN_VISIBILITY_DEFAULT MatchDecider {
00596     public:
00599         virtual bool operator()(const Xapian::Document &doc) const = 0;
00600 
00602         virtual ~MatchDecider();
00603 };
00604 
00615 class XAPIAN_VISIBILITY_DEFAULT Enquire {
00616     private:
00618         Enquire(const Enquire &);
00619 
00621         void operator=(const Enquire &);
00622 
00623     public:
00624         class Internal;
00626         Xapian::Internal::RefCntPtr<Internal> internal;
00627 
00652         explicit Enquire(const Database &database, ErrorHandler * errorhandler_ = 0);
00653 
00656         ~Enquire();
00657 
00664         void set_query(const Xapian::Query & query, Xapian::termcount qlen = 0);
00665 
00672         const Xapian::Query & get_query() const;
00673 
00680         void set_weighting_scheme(const Weight &weight_);
00681 
00708         void set_collapse_key(Xapian::valueno collapse_key);
00709 
00710         typedef enum {
00711             ASCENDING = 1,
00712             DESCENDING = 0,
00713             DONT_CARE = 2
00714         } docid_order;
00715 
00739         void set_docid_order(docid_order order);
00740 
00759         void set_cutoff(Xapian::percent percent_cutoff, Xapian::weight weight_cutoff = 0);
00760 
00765         void set_sort_by_relevance();
00766 
00779         void set_sort_by_value(Xapian::valueno sort_key, bool ascending = true);
00780 
00794         void set_sort_by_value_then_relevance(Xapian::valueno sort_key,
00795                                               bool ascending = true);
00796 
00816         void set_sort_by_relevance_then_value(Xapian::valueno sort_key,
00817                                               bool ascending = true);
00818 
00851         MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00852                       Xapian::doccount checkatleast = 0,
00853                       const RSet * omrset = 0,
00854                       const MatchDecider * mdecider = 0) const;
00855         MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00856                       Xapian::doccount checkatleast,
00857                       const RSet * omrset,
00858                       const MatchDecider * mdecider,
00859                       const MatchDecider * matchspy) const;
00860         MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems,
00861                       const RSet * omrset,
00862                       const MatchDecider * mdecider = 0) const {
00863             return get_mset(first, maxitems, 0, omrset, mdecider);
00864         }
00865 
00866         static const int INCLUDE_QUERY_TERMS = 1;
00867         static const int USE_EXACT_TERMFREQ = 2;
00868 #ifndef _MSC_VER
00870         XAPIAN_DEPRECATED(static const int include_query_terms) = 1;
00872         XAPIAN_DEPRECATED(static const int use_exact_termfreq) = 2;
00873 #else
00874         // Work around MSVC stupidity (you get a warning for deprecating a
00875         // declaration).
00876         static const int include_query_terms = 1;
00877         static const int use_exact_termfreq = 2;
00878 #pragma deprecated("Xapian::Enquire::include_query_terms", "Xapian::Enquire::use_exact_termfreq")
00879 #endif
00880 
00903         ESet get_eset(Xapian::termcount maxitems,
00904                         const RSet & omrset,
00905                         int flags = 0,
00906                         double k = 1.0,
00907                         const Xapian::ExpandDecider * edecider = 0) const;
00908 
00922         inline ESet get_eset(Xapian::termcount maxitems, const RSet & omrset,
00923                                const Xapian::ExpandDecider * edecider) const {
00924             return get_eset(maxitems, omrset, 0, 1.0, edecider);
00925         }
00926 
00955         TermIterator get_matching_terms_begin(Xapian::docid did) const;
00956 
00958         TermIterator get_matching_terms_end(Xapian::docid /*did*/) const {
00959             return TermIterator(NULL);
00960         }
00961 
00984         TermIterator get_matching_terms_begin(const MSetIterator &it) const;
00985 
00987         TermIterator get_matching_terms_end(const MSetIterator &/*it*/) const {
00988             return TermIterator(NULL);
00989         }
00990 
00997         void register_match_decider(const std::string &name,
00998                                     const MatchDecider *mdecider = NULL);
00999 
01003         std::string get_description() const;
01004 };
01005 
01006 }
01007 
01008 class RemoteServer;
01009 
01010 namespace Xapian {
01011 
01013 class XAPIAN_VISIBILITY_DEFAULT Weight {
01014     friend class Enquire; // So Enquire can clone us
01015     friend class ::RemoteServer; // So RemoteServer can clone us - FIXME
01016     public:
01017         class Internal;
01018     protected:
01019         Weight(const Weight &);
01020     private:
01021         void operator=(Weight &);
01022 
01032         virtual Weight * clone() const = 0;
01033 
01034     protected:
01035         const Internal * internal; // Weight::Internal == StatsSource
01036         Xapian::doclength querysize;
01037         Xapian::termcount wqf;
01038         std::string tname;
01039 
01040     public:
01041         Weight() { }
01042         virtual ~Weight();
01043 
01056         Weight * create(const Internal * internal_, Xapian::doclength querysize_,
01057                         Xapian::termcount wqf_, const std::string & tname_) const;
01058 
01063         virtual std::string name() const = 0;
01064 
01066         virtual std::string serialise() const = 0;
01067 
01069         virtual Weight * unserialise(const std::string &s) const = 0;
01070 
01078         virtual Xapian::weight get_sumpart(Xapian::termcount wdf,
01079                                       Xapian::doclength len) const = 0;
01080 
01086         virtual Xapian::weight get_maxpart() const = 0;
01087 
01096         virtual Xapian::weight get_sumextra(Xapian::doclength len) const = 0;
01097 
01101         virtual Xapian::weight get_maxextra() const = 0;
01102 
01104         virtual bool get_sumpart_needs_doclength() const; /* { return true; } */
01105 };
01106 
01108 class XAPIAN_VISIBILITY_DEFAULT BoolWeight : public Weight {
01109     public:
01110         BoolWeight * clone() const;
01111         BoolWeight() { }
01112         ~BoolWeight();
01113         std::string name() const;
01114         std::string serialise() const;
01115         BoolWeight * unserialise(const std::string & s) const;
01116         Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01117         Xapian::weight get_maxpart() const;
01118 
01119         Xapian::weight get_sumextra(Xapian::doclength len) const;
01120         Xapian::weight get_maxextra() const;
01121 
01122         bool get_sumpart_needs_doclength() const;
01123 };
01124 
01137 class XAPIAN_VISIBILITY_DEFAULT BM25Weight : public Weight {
01138     private:
01139         mutable Xapian::weight termweight;
01140         mutable Xapian::doclength lenpart;
01141 
01142         double k1, k2, k3, b;
01143         Xapian::doclength min_normlen;
01144 
01145         mutable bool weight_calculated;
01146 
01147         void calc_termweight() const;
01148 
01149     public:
01168         BM25Weight(double k1_, double k2_, double k3_, double b_,
01169                    double min_normlen_)
01170                 : k1(k1_), k2(k2_), k3(k3_), b(b_), min_normlen(min_normlen_),
01171                   weight_calculated(false)
01172         {
01173             if (k1 < 0) k1 = 0;
01174             if (k2 < 0) k2 = 0;
01175             if (k3 < 0) k3 = 0;
01176             if (b < 0) b = 0; else if (b > 1) b = 1;
01177         }
01178         BM25Weight() : k1(1), k2(0), k3(1), b(0.5), min_normlen(0.5),
01179                        weight_calculated(false) { }
01180 
01181         BM25Weight * clone() const;
01182         ~BM25Weight() { }
01183         std::string name() const;
01184         std::string serialise() const;
01185         BM25Weight * unserialise(const std::string & s) const;
01186         Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01187         Xapian::weight get_maxpart() const;
01188 
01189         Xapian::weight get_sumextra(Xapian::doclength len) const;
01190         Xapian::weight get_maxextra() const;
01191 
01192         bool get_sumpart_needs_doclength() const;
01193 };
01194 
01212 class XAPIAN_VISIBILITY_DEFAULT TradWeight : public Weight {
01213     private:
01214         mutable Xapian::weight termweight;
01215         mutable Xapian::doclength lenpart;
01216 
01217         double param_k;
01218 
01219         mutable bool weight_calculated;
01220 
01221         void calc_termweight() const;
01222 
01223     public:
01231         explicit TradWeight(double k) : param_k(k), weight_calculated(false) {
01232             if (param_k < 0) param_k = 0;
01233         }
01234 
01235         TradWeight() : param_k(1.0), weight_calculated(false) { }
01236 
01237         TradWeight * clone() const;
01238         ~TradWeight() { }
01239         std::string name() const;
01240         std::string serialise() const;
01241         TradWeight * unserialise(const std::string & s) const;
01242 
01243         Xapian::weight get_sumpart(Xapian::termcount wdf, Xapian::doclength len) const;
01244         Xapian::weight get_maxpart() const;
01245 
01246         Xapian::weight get_sumextra(Xapian::doclength len) const;
01247         Xapian::weight get_maxextra() const;
01248 
01249         bool get_sumpart_needs_doclength() const;
01250 };
01251 
01252 }
01253 
01254 #endif /* XAPIAN_INCLUDED_ENQUIRE_H */

Documentation for Xapian (version 1.0.2).
Generated on 5 Jul 2007 by Doxygen 1.5.2.