xapian-core  2.1.0
mset.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2015,2016,2017,2019,2023,2024,2026 Olly Betts
5  * Copyright (C) 2018 Uppinder Chugh
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef XAPIAN_INCLUDED_MSET_H
23 #define XAPIAN_INCLUDED_MSET_H
24 
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error Never use <xapian/mset.h> directly; include <xapian.h> instead.
27 #endif
28 
29 #include <iterator>
30 #include <string>
31 #include <string_view>
32 
33 #include <xapian/attributes.h>
34 #include <xapian/document.h>
35 #include <xapian/error.h>
36 #include <xapian/intrusive_ptr.h>
37 #include <xapian/stem.h>
38 #include <xapian/types.h>
39 #include <xapian/visibility.h>
40 
41 namespace Xapian {
42 
43 class MSetIterator;
44 
47  friend class MSetIterator;
48 
49  // Helper function for fetch() methods.
50  void fetch_(Xapian::doccount first, Xapian::doccount last) const;
51 
66  void set_item_weight(Xapian::doccount i, double wt);
67 
68 #if 0 // FIXME: Need work before release.
70  void diversify_(Xapian::doccount k,
72  double factor1,
73  double factor2);
74 #endif
75 
76  public:
78  class Internal;
81 
86  MSet(const MSet & o);
87 
92  MSet & operator=(const MSet & o);
93 
95  MSet(MSet && o);
96 
98  MSet & operator=(MSet && o);
99 
104  MSet();
105 
108  explicit MSet(Internal* internal_);
109 
111  ~MSet();
112 
129  template<typename Iterator>
130  void replace_weights(Iterator first, Iterator last)
131  {
132  auto distance = last - first;
133  // Take care to compare signed and unsigned types both safely and
134  // without triggering compiler warnings.
135  if (distance < 0 ||
136  (sizeof(distance) <= sizeof(Xapian::doccount) ?
137  Xapian::doccount(distance) != size() :
138  distance != static_cast<decltype(distance)>(size()))) {
139  throw Xapian::InvalidArgumentError("Number of weights assigned "
140  "doesn't match the number of "
141  "items");
142  }
143  Xapian::doccount i = 0;
144  while (first != last) {
145  set_item_weight(i, *first);
146  ++i;
147  ++first;
148  }
149  }
150 
159  void sort_by_relevance();
160 
161 #if 0 // FIXME: Need work before release.
181  void diversify(Xapian::doccount k,
183  double lambda = 0.5,
184  double b = 5.0,
185  double sigma_sqr = 1e-3) {
186  // Inline the argument value checks and the calculation of the scale
187  // factor for score_2 so the compiler can optimise in the case where
188  // some or all parameter values are compile-time constants.
189  if (r == 0)
190  throw InvalidArgumentError("r must be > 0");
191  if (lambda < 0.0 || lambda > 1.0)
192  throw InvalidArgumentError("lambda must be between 0 and 1");
193  if (k > 1)
194  diversify_(k, r, lambda, (1.0 - lambda) * b * sigma_sqr * 2.0);
195  }
196 #endif
197 
222  int convert_to_percent(double weight) const;
223 
248  int convert_to_percent(const MSetIterator & it) const;
249 
264  Xapian::doccount get_termfreq(std::string_view term) const;
265 
278  double get_termweight(std::string_view term) const;
279 
284  Xapian::doccount get_firstitem() const;
285 
287  Xapian::doccount get_matches_lower_bound() const;
289  Xapian::doccount get_matches_estimated() const;
291  Xapian::doccount get_matches_upper_bound() const;
292 
298  Xapian::doccount get_uncollapsed_matches_lower_bound() const;
304  Xapian::doccount get_uncollapsed_matches_estimated() const;
310  Xapian::doccount get_uncollapsed_matches_upper_bound() const;
311 
313  double get_max_attained() const;
315  double get_max_possible() const;
316 
317  enum {
324  SNIPPET_BACKGROUND_MODEL = 1,
331  SNIPPET_EXHAUSTIVE = 2,
338  SNIPPET_EMPTY_WITHOUT_MATCH = 4,
339 
363  SNIPPET_NGRAMS = 2048,
364 
372  SNIPPET_CJK_NGRAM = SNIPPET_NGRAMS,
373 
387  SNIPPET_WORD_BREAKS = 4096
388  };
389 
423  std::string snippet(std::string_view text,
424  size_t length = 500,
425  const Xapian::Stem & stemmer = Xapian::Stem(),
426  unsigned flags = SNIPPET_BACKGROUND_MODEL|SNIPPET_EXHAUSTIVE,
427  std::string_view hi_start = "<b>",
428  std::string_view hi_end = "</b>",
429  std::string_view omit = "...") const;
430 
441  void fetch(const MSetIterator &begin, const MSetIterator &end) const;
442 
453  void fetch(const MSetIterator &item) const;
454 
465  void fetch() const { fetch_(0, Xapian::doccount(-1)); }
466 
468  Xapian::doccount size() const;
469 
471  bool empty() const { return size() == 0; }
472 
474  void swap(MSet & o) { internal.swap(o.internal); }
475 
477  MSetIterator begin() const;
478 
480  MSetIterator end() const;
481 
483  MSetIterator operator[](Xapian::doccount i) const;
484 
486  MSetIterator back() const;
487 
489  std::string get_description() const;
490 
502  // @{
514  typedef value_type * pointer;
516  typedef const value_type * const_pointer;
521  // @}
522  //
532  // @{
533  // The size is fixed once created.
534  Xapian::doccount max_size() const { return size(); }
535  // @}
536 };
537 
540  friend class MSet;
541 
542  MSetIterator(const Xapian::MSet & mset_, Xapian::doccount off_from_end_)
543  : mset(mset_), off_from_end(off_from_end_) { }
544 
545  public:
548 
555 
557  MSetIterator() : off_from_end(0) { }
558 
560  Xapian::docid operator*() const;
561 
564  --off_from_end;
565  return *this;
566  }
567 
570  MSetIterator retval = *this;
571  --off_from_end;
572  return retval;
573  }
574 
577  ++off_from_end;
578  return *this;
579  }
580 
583  MSetIterator retval = *this;
584  ++off_from_end;
585  return retval;
586  }
587 
598  // @{
600  typedef std::random_access_iterator_tag iterator_category;
606  typedef value_type* pointer;
609  // @}
610 
613  off_from_end -= n;
614  return *this;
615  }
616 
619  off_from_end += n;
620  return *this;
621  }
622 
628  return MSetIterator(mset, off_from_end - n);
629  }
630 
636  return MSetIterator(mset, off_from_end + n);
637  }
638 
641  return difference_type(o.off_from_end) - difference_type(off_from_end);
642  }
643 
649  return mset.get_firstitem() + (mset.size() - off_from_end);
650  }
651 
653  Xapian::Document get_document() const;
654 
656  double get_weight() const;
657 
662  std::string get_collapse_key() const;
663 
681  Xapian::doccount get_collapse_count() const;
682 
689  std::string get_sort_key() const;
690 
715  int get_percent() const {
716  return mset.convert_to_percent(get_weight());
717  }
718 
720  std::string get_description() const;
721 };
722 
724 inline bool
725 operator==(const MSetIterator& a, const MSetIterator& b) noexcept
726 {
727  return a.off_from_end == b.off_from_end;
728 }
729 
731 inline bool
732 operator!=(const MSetIterator& a, const MSetIterator& b) noexcept
733 {
734  return !(a == b);
735 }
736 
738 inline bool
739 operator<(const MSetIterator& a, const MSetIterator& b) noexcept
740 {
741  return a.off_from_end > b.off_from_end;
742 }
743 
745 inline bool
746 operator>(const MSetIterator& a, const MSetIterator& b) noexcept
747 {
748  return b < a;
749 }
750 
752 inline bool
753 operator>=(const MSetIterator& a, const MSetIterator& b) noexcept
754 {
755  return !(a < b);
756 }
757 
759 inline bool
760 operator<=(const MSetIterator& a, const MSetIterator& b) noexcept
761 {
762  return !(b < a);
763 }
764 
769 inline MSetIterator
771 {
772  return it + n;
773 }
774 
775 // Inlined methods of MSet which need MSetIterator to have been defined:
776 
777 inline void
778 MSet::fetch(const MSetIterator &begin_it, const MSetIterator &end_it) const
779 {
780  fetch_(begin_it.off_from_end, end_it.off_from_end);
781 }
782 
783 inline void
784 MSet::fetch(const MSetIterator &item) const
785 {
786  fetch_(item.off_from_end, item.off_from_end);
787 }
788 
789 inline MSetIterator
790 MSet::begin() const {
791  return MSetIterator(*this, size());
792 }
793 
794 inline MSetIterator
795 MSet::end() const {
796  // Decrementing the result of end() needs to work, so we must pass in
797  // *this here.
798  return MSetIterator(*this, 0);
799 }
800 
801 inline MSetIterator
803  return MSetIterator(*this, size() - i);
804 }
805 
806 inline MSetIterator
807 MSet::back() const {
808  return MSetIterator(*this, 1);
809 }
810 
811 inline int
813  return convert_to_percent(it.get_weight());
814 }
815 
816 }
817 
818 #endif // XAPIAN_INCLUDED_MSET_H
Compiler attribute macros.
Class representing a document.
Definition: document.h:64
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:229
Iterator over a Xapian::MSet.
Definition: mset.h:539
MSetIterator operator-(difference_type n) const
Return the iterator decremented by n positions.
Definition: mset.h:635
Xapian::docid value_type
Definition: mset.h:602
MSetIterator & operator+=(difference_type n)
Move the iterator forwards by n positions.
Definition: mset.h:612
MSetIterator()
Create an unpositioned MSetIterator.
Definition: mset.h:557
std::random_access_iterator_tag iterator_category
Definition: mset.h:600
Xapian::doccount get_rank() const
Return the MSet rank for the current position.
Definition: mset.h:648
MSetIterator & operator--()
Move the iterator to the previous position.
Definition: mset.h:576
int get_percent() const
Convert the weight of the current iterator position to a percentage.
Definition: mset.h:715
Xapian::MSet::size_type off_from_end
Definition: mset.h:554
MSetIterator & operator-=(difference_type n)
Move the iterator back by n positions.
Definition: mset.h:618
Xapian::termcount_diff difference_type
Definition: mset.h:604
MSetIterator operator+(difference_type n) const
Return the iterator incremented by n positions.
Definition: mset.h:627
value_type * pointer
Definition: mset.h:606
double get_weight() const
Get the weight for the current position.
Definition: msetiterator.cc:55
MSetIterator(const Xapian::MSet &mset_, Xapian::doccount off_from_end_)
Definition: mset.h:542
Xapian::MSet mset
Definition: mset.h:547
MSetIterator & operator++()
Advance the iterator to the next position.
Definition: mset.h:563
MSetIterator operator--(int)
Move the iterator to the previous position (postfix version).
Definition: mset.h:582
difference_type operator-(const MSetIterator &o) const
Return the number of positions between o and this iterator.
Definition: mset.h:640
value_type reference
Definition: mset.h:608
MSetIterator operator++(int)
Advance the iterator to the next position (postfix version).
Definition: mset.h:569
Xapian::MSet internals.
Definition: msetinternal.h:44
Class representing a list of search results.
Definition: mset.h:46
Xapian::Internal::intrusive_ptr_nonnull< Internal > internal
Definition: mset.h:78
value_type * pointer
Definition: mset.h:514
MSet(MSet &&o)
Move constructor.
Xapian::doccount_diff difference_type
Definition: mset.h:508
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: mset.cc:375
value_type reference
Definition: mset.h:518
MSet(const MSet &o)
Copying is allowed.
void fetch() const
Prefetch hint the whole MSet.
Definition: mset.h:465
void replace_weights(Iterator first, Iterator last)
Assigns new weights and updates MSet.
Definition: mset.h:130
Xapian::doccount max_size() const
Definition: mset.h:534
void fetch_(Xapian::doccount first, Xapian::doccount last) const
Definition: mset.cc:67
Xapian::doccount size_type
Definition: mset.h:506
Xapian::MSetIterator value_type
Definition: mset.h:504
friend class MSetIterator
Definition: mset.h:47
MSet & operator=(MSet &&o)
Move assignment operator.
int convert_to_percent(double weight) const
Convert a weight to a percentage.
Definition: mset.cc:276
Xapian::MSetIterator iterator
Definition: mset.h:510
bool empty() const
Return true if this MSet object is empty.
Definition: mset.h:471
MSet & operator=(const MSet &o)
Copying is allowed.
Xapian::doccount get_firstitem() const
Rank of first item in this MSet.
Definition: mset.cc:313
MSetIterator operator[](Xapian::doccount i) const
Return iterator pointing to the i-th object in this MSet.
Definition: mset.h:802
MSetIterator back() const
Return iterator pointing to the last object in this MSet.
Definition: mset.h:807
void swap(MSet &o)
Efficiently swap this MSet object with another.
Definition: mset.h:474
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:790
const value_type const_reference
Definition: mset.h:520
Xapian::MSetIterator const_iterator
Definition: mset.h:512
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:795
const value_type * const_pointer
Definition: mset.h:516
Class representing a stemming algorithm.
Definition: stem.h:74
string term
Class representing a document.
Hierarchy of classes which Xapian can throw as exceptions.
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
XAPIAN_DOCID_BASE_TYPE doccount_diff
A signed difference between two counts of documents.
Definition: types.h:44
bool operator>(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:292
XAPIAN_TERMCOUNT_BASE_TYPE termcount_diff
A signed difference between two counts of terms.
Definition: types.h:71
bool operator>=(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:299
bool operator<=(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:306
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
bool operator==(const ESetIterator &a, const ESetIterator &b) noexcept
Equality test for ESetIterator objects.
Definition: eset.h:271
bool operator!=(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:278
ESetIterator operator+(ESetIterator::difference_type n, const ESetIterator &it)
Return ESetIterator it incremented by n positions.
Definition: eset.h:316
bool operator<(const ESetIterator &a, const ESetIterator &b) noexcept
Inequality test for ESetIterator objects.
Definition: eset.h:285
const Query operator*(double factor, const Query &q)
Scale a Xapian::Query object using OP_SCALE_WEIGHT.
Definition: query.h:827
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
stemming algorithms
static Xapian::Stem stemmer
Definition: stemtest.cc:42
typedefs for Xapian
Define XAPIAN_VISIBILITY_* macros.
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
#define XAPIAN_VISIBILITY_INTERNAL
Definition: visibility.h:29