2 * @brief Class representing a list of search results
4 /* Copyright (C) 2015,2016 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #ifndef XAPIAN_INCLUDED_MSET_H
23 #define XAPIAN_INCLUDED_MSET_H
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error "Never use <xapian/mset.h> directly; include <xapian.h> instead."
32 #include <xapian/attributes.h>
33 #include <xapian/document.h>
34 #include <xapian/intrusive_ptr.h>
35 #include <xapian/stem.h>
36 #include <xapian/types.h>
37 #include <xapian/visibility.h>
43 /// Class representing a list of search results.
44 class XAPIAN_VISIBILITY_DEFAULT MSet
{
45 friend class MSetIterator
;
47 // Helper function for fetch() methods.
48 void fetch_(Xapian::doccount first
, Xapian::doccount last
) const;
51 /// Class representing the MSet internals.
53 /// @private @internal Reference counted internals.
54 Xapian::Internal::intrusive_ptr
<Internal
> internal
;
56 /** Copying is allowed.
58 * The internals are reference counted, so copying is cheap.
62 /** Copying is allowed.
64 * The internals are reference counted, so assignment is cheap.
66 MSet
& operator=(const MSet
& o
);
68 /** Default constructor.
70 * Creates an empty MSet, mostly useful as a placeholder.
77 int convert_to_percent(double weight
) const;
79 int convert_to_percent(const MSetIterator
& it
) const;
81 Xapian::doccount
get_termfreq(const std::string
& term
) const;
83 double get_termweight(const std::string
& term
) const;
85 Xapian::doccount
get_firstitem() const;
87 Xapian::doccount
get_matches_lower_bound() const;
88 Xapian::doccount
get_matches_estimated() const;
89 Xapian::doccount
get_matches_upper_bound() const;
91 Xapian::doccount
get_uncollapsed_matches_lower_bound() const;
92 Xapian::doccount
get_uncollapsed_matches_estimated() const;
93 Xapian::doccount
get_uncollapsed_matches_upper_bound() const;
95 double get_max_attained() const;
96 double get_max_possible() const;
99 SNIPPET_BACKGROUND_MODEL
= 1,
100 SNIPPET_EXHAUSTIVE
= 2
103 /** Generate a snippet.
105 * This method selects a continuous run of words of less than about @a
106 * length bytes from @a text, based mainly on where the query matches
107 * (currently terms, exact phrases and wildcards are taken into account),
108 * but also on the non-query terms in the text.
110 * The returned text can be escaped (by default, it is escaped to make it
111 * suitable for use in HTML), and matches with the query will be
112 * highlighted using @a hi_start and @a hi_end.
114 * If the snippet seems to start or end mid-sentence, then @a omit is
115 * prepended or append (respectively) to indicate this.
117 * The stemmer used to build the query should be specified in @a stemmer.
119 * And @a flags contains flags controlling behaviour.
121 std::string
snippet(const std::string
& text
,
123 const Xapian::Stem
& stemmer
= Xapian::Stem(),
124 unsigned flags
= SNIPPET_BACKGROUND_MODEL
|SNIPPET_EXHAUSTIVE
,
125 const std::string
& hi_start
= "<b>",
126 const std::string
& hi_end
= "</b>",
127 const std::string
& omit
= "...") const;
129 /** Prefetch hint a range of items.
131 * For a remote database, this may start a pipelined fetched of the
132 * requested documents from the remote server.
134 * For a disk-based database, this may send prefetch hints to the
135 * operating system such that the disk blocks the requested documents
136 * are stored in are more likely to be in the cache when we come to
137 * actually read them.
139 void fetch(const MSetIterator
&begin
, const MSetIterator
&end
) const;
141 /** Prefetch hint a single MSet item.
143 * For a remote database, this may start a pipelined fetched of the
144 * requested documents from the remote server.
146 * For a disk-based database, this may send prefetch hints to the
147 * operating system such that the disk blocks the requested documents
148 * are stored in are more likely to be in the cache when we come to
149 * actually read them.
151 void fetch(const MSetIterator
&item
) const;
153 /** Prefetch hint the whole MSet.
155 * For a remote database, this may start a pipelined fetched of the
156 * requested documents from the remote server.
158 * For a disk-based database, this may send prefetch hints to the
159 * operating system such that the disk blocks the requested documents
160 * are stored in are more likely to be in the cache when we come to
161 * actually read them.
163 void fetch() const { fetch_(0, Xapian::doccount(-1)); }
165 Xapian::doccount
size() const;
167 bool empty() const { return size() == 0; }
169 void swap(MSet
& o
) { internal
.swap(o
.internal
); }
171 MSetIterator
begin() const;
173 MSetIterator
end() const;
175 MSetIterator
operator[](Xapian::doccount i
) const;
177 MSetIterator
back() const;
179 /// Return a string describing this object.
180 std::string
get_description() const;
182 /** @private @internal MSet is what the C++ STL calls a container.
184 * The following typedefs allow the class to be used in templates in the
185 * same way the standard containers can be.
187 * These are deliberately hidden from the Doxygen-generated docs, as the
188 * machinery here isn't interesting to API users. They just need to know
189 * that Xapian container classes are compatible with the STL.
191 * See "The C++ Programming Language", 3rd ed. section 16.3.1:
195 typedef Xapian::MSetIterator value_type
;
197 typedef Xapian::doccount size_type
;
199 typedef Xapian::doccount_diff difference_type
;
201 typedef Xapian::MSetIterator iterator
;
203 typedef Xapian::MSetIterator const_iterator
;
205 typedef value_type
* pointer
;
207 typedef const value_type
* const_pointer
;
209 typedef value_type
& reference
;
211 typedef const value_type
& const_reference
;
214 /** @private @internal MSet is what the C++ STL calls a container.
216 * The following methods allow the class to be used in templates in the
217 * same way the standard containers can be.
219 * These are deliberately hidden from the Doxygen-generated docs, as the
220 * machinery here isn't interesting to API users. They just need to know
221 * that Xapian container classes are compatible with the STL.
224 // The size is fixed once created.
225 Xapian::doccount
max_size() const { return size(); }
229 /// Iterator over a Xapian::MSet.
230 class XAPIAN_VISIBILITY_DEFAULT MSetIterator
{
233 MSetIterator(const Xapian::MSet
& mset_
, Xapian::doccount off_from_end_
)
234 : mset(mset_
), off_from_end(off_from_end_
) { }
237 /** @private @internal The MSet we are iterating over. */
240 /** @private @internal The current position of the iterator.
242 * We store the offset from the end of @a mset, since that means
243 * MSet::end() just needs to set this member to 0.
245 Xapian::MSet::size_type off_from_end
;
247 /** Create an unpositioned MSetIterator. */
248 MSetIterator() : off_from_end(0) { }
250 /** Get the numeric document id for the current position. */
251 Xapian::docid
operator*() const;
253 /// Advance the iterator to the next position.
254 MSetIterator
& operator++() {
259 /// Advance the iterator to the next position (postfix version).
260 MSetIterator
operator++(int) {
261 MSetIterator retval
= *this;
266 /// Move the iterator to the previous position.
267 MSetIterator
& operator--() {
272 /// Move the iterator to the previous position (postfix version).
273 MSetIterator
operator--(int) {
274 MSetIterator retval
= *this;
279 Xapian::doccount
get_rank() const {
280 return mset
.get_firstitem() + (mset
.size() - off_from_end
);
283 /** Get the Document object for the current position. */
284 Xapian::Document
get_document() const;
286 /** Get the weight for the current position. */
287 double get_weight() const;
289 std::string
get_collapse_key() const;
291 Xapian::doccount
get_collapse_count() const;
293 int get_percent() const {
294 return mset
.convert_to_percent(get_weight());
297 /// Return a string describing this object.
298 std::string
get_description() const;
302 XAPIAN_NOTHROW(operator==(const MSetIterator
&a
, const MSetIterator
&b
));
304 /// Equality test for MSetIterator objects.
306 operator==(const MSetIterator
&a
, const MSetIterator
&b
) XAPIAN_NOEXCEPT
308 // Use a pointer comparison - this ensures both that (a == a) and correct
309 // handling of end iterators (which we ensure have NULL internals).
310 return a
.off_from_end
== b
.off_from_end
;
314 XAPIAN_NOTHROW(operator!=(const MSetIterator
&a
, const MSetIterator
&b
));
316 /// Inequality test for MSetIterator objects.
318 operator!=(const MSetIterator
&a
, const MSetIterator
&b
) XAPIAN_NOEXCEPT
323 // Inlined methods of MSet which need MSetIterator to have been defined:
326 MSet::fetch(const MSetIterator
&begin
, const MSetIterator
&end
) const
328 fetch_(begin
.off_from_end
, end
.off_from_end
);
332 MSet::fetch(const MSetIterator
&item
) const
334 fetch_(item
.off_from_end
, item
.off_from_end
);
338 MSet::begin() const {
339 return MSetIterator(*this, size());
344 // Decrementing the result of end() needs to work, so we must pass in
346 return MSetIterator(*this, 0);
350 MSet::operator[](Xapian::doccount i
) const {
351 return MSetIterator(*this, size() - i
);
356 return MSetIterator(*this, 1);
361 #endif // XAPIAN_INCLUDED_POSTINGITERATOR_H